package zero1hd.polyjet.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
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 zero1hd.polyjet.Polyjet;
import zero1hd.polyjet.audio.Audio;
import zero1hd.polyjet.audio.WavAudioData;
import zero1hd.polyjet.ui.pages.AnalyzePage;
import zero1hd.polyjet.ui.pages.MusicSelectionPage;
import zero1hd.polyjet.util.MiniEvents;
import zero1hd.polyjet.util.MiniListener;
import zero1hd.polyjet.util.TransitionAdapter;


public class PreGameScreen extends ScreenAdapter implements TransitionAdapter, MiniListener {
	Polyjet core;
	Stage stage;
	Label statusText;

	WavAudioData audiofile;
	Vector3 cameraTarget;
	
	Label lastStatement;
	
	private MainMenu mainMenu;
	private MusicSelectionPage musicSelection;
	private AnalyzePage analyzePage;
	public PreGameScreen(final Polyjet core, MainMenu mainMenu) {
		this.core = core;
		
		this.mainMenu = mainMenu;
		
		stage = new Stage();
		
		cameraTarget = new Vector3(stage.getCamera().position);
		postTransition();
	}
	
	@Override
	public void postTransition() {
		stage.clear();
		
		Image cyberCircle1 = new Image(core.getAssetManager().get("cybercircle3B.png", Texture.class));
		cyberCircle1.setScale(0.8f);
		cyberCircle1.setOrigin(cyberCircle1.getWidth()/2, cyberCircle1.getHeight()/2);
		cyberCircle1.setColor(0.8f,0.8f,0.8f,0.7f);
		cyberCircle1.addAction(Actions.forever(Actions.rotateBy(360f, 10f)));
		cyberCircle1.setPosition(Gdx.graphics.getWidth()-cyberCircle1.getWidth()/2-10, -cyberCircle1.getHeight()*2/4f);
		stage.addActor(cyberCircle1);
		
		analyzePage = new AnalyzePage(core.getDefaultSkin(), cameraTarget, core.getAssetManager());
		analyzePage.setPosition(1f*Gdx.graphics.getWidth(), 0);
		stage.addActor(analyzePage);
		
		//draw music selector
		musicSelection = new MusicSelectionPage(core, mainMenu);
		musicSelection.addListener(this);
		stage.addActor(musicSelection);
		musicSelection.beginMusicSearch();
		
	}
	
	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		stage.getViewport().apply();
		stage.act();
		stage.draw();
		
		if (stage.getCamera().position.x != cameraTarget.x) {
			stage.getCamera().position.lerp(cameraTarget, 0.25f);
		}
		
		if (cameraTarget.x != 1.5f*Gdx.graphics.getWidth() && analyzePage.hasAudioData()) {
			cameraTarget.x = 1.5f*Gdx.graphics.getWidth();
		}
		super.render(delta);
	}
	
	@Override
	public void show() {
		Gdx.input.setInputProcessor(stage);
		super.show();
	}
	
	@Override
	public void resize(int width, int height) {
		stage.getViewport().update(width, height);
		super.resize(width, height);
	}
	
	@Override
	public void dispose() {
		stage.dispose();
		super.dispose();
	}

	@Override
	public void handle(MiniEvents ID) {
		switch (ID) {
		case MUSIC_SELECTED:
			analyzePage.setSong(Audio.getAudioData(musicSelection.getSelectedMusic()), musicSelection.getSelectedMusicInfo());
			break;
		default:
			break;
		}
	}
}
