package zero1hd.polyjet.ui.pages;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;

import zero1hd.polyjet.audio.AudioAnalyzer;
import zero1hd.polyjet.audio.AudioData;
import zero1hd.polyjet.audio.AudioInfo;

public class AnalyzePage extends Page {
	AudioAnalyzer audioAnalyzer;
	AudioData music;
	
	Vector3 cameraPos;
	
	Table songInfo;
	Slider difficultyModifier;
	Label difficultyModifierPercentage;
	private Skin skin;
	
	private TextButton back;
	private Image loadingCircle;
	
	private Label status;
	public AnalyzePage(Skin skin, Vector3 camPos, AssetManager assets) {
		super("Results", skin);
		this.skin = skin;
		cameraPos = camPos;
		audioAnalyzer = new AudioAnalyzer();
		songInfo = new Table(skin);
		
		loadingCircle = new Image(assets.get("cybercircle1.png", Texture.class));
		loadingCircle.setPosition((getWidth()-loadingCircle.getWidth())/2, (getHeightBelowTitle()-loadingCircle.getHeight())/2);
		loadingCircle.setColor(0.8f,0.8f,0.8f,0.7f);
		loadingCircle.setOrigin(loadingCircle.getWidth()/2, loadingCircle.getHeight()/2);
		loadingCircle.addAction(Actions.forever(Actions.rotateBy(-360f, 4f)));
		addActor(loadingCircle);
		
		back = new TextButton("Back", skin);
		back.setPosition(getWidth()-back.getWidth()-15f, getHeightBelowTitle());
		back.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				clearAudioData();
				cameraPos.x = 0.5f*getWidth();
			}
		});
		songInfo.setSize(getWidth(), getHeightBelowTitle());

		status = new Label(null, skin, "sub-font", skin.getColor("default"));
		
		addActor(back);
	}
	
	public void setSong(AudioData music, AudioInfo audioInfo) {
		songInfo.clear();
		
		cameraPos.x = 1.5f*getWidth();
		this.music = music;
		audioAnalyzer.startAnalyticalThread(music);
		songInfo.add(new Label(audioInfo.getSongName(), skin, "large-font", skin.getColor("default")));
		songInfo.row();
		songInfo.add(status);
		addActor(songInfo);
		
	}
	
	@Override
	public void act(float delta) {
		status.setText("Initial analysis: " + audioAnalyzer.getProgress());
		super.act(delta);
	}
	
	public boolean hasAudioData() {
		if (music != null) {
			return true;
		} else {
			return false;
		}
	}
	public void clearAudioData() {
		music = null;
	}
	
	public AudioAnalyzer getAudioAnalyzer() {
		return audioAnalyzer;
	}
}
