package zero1hd.polyjet.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
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.ui.Image;
import com.badlogic.gdx.utils.viewport.ScreenViewport;

import zero1hd.polyjet.Polyjet;
import zero1hd.polyjet.audio.Audio;
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 {
	Stage stage;
	
	MusicSelectionPage ms;
	AnalyzePage ap;
	private Vector3 cameraPos;
	private Polyjet core;
	
	public PreGameScreen(Polyjet core) {
		stage = new Stage(new ScreenViewport());
		cameraPos = new Vector3(stage.getCamera().position);
		this.core = core;
	}
	
	@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 != cameraPos.x) {
			stage.getCamera().position.lerp(cameraPos, 0.25f);
		}
		super.render(delta);
	}
	
	@Override
	public void resize(int width, int height) {
		stage.getViewport().update(width, height);
		super.resize(width, height);
	}
	
	@Override
	public void handle(MiniEvents ID) {
		switch (ID) {
		case MUSIC_SELECTED:
			cameraPos.x = 1.5f*Gdx.graphics.getWidth();
			ap.setSong(Audio.getAudioData(ms.getSelectedMusic()), ms.getSelectedMusicInfo(), this);
			break;
		case SPECTRAL_FLUX_DONE:
			break;
		case MUSIC_DATA_CLEANED:
			break;
		case BACK:
			if (cameraPos.x == 1.5f*Gdx.graphics.getWidth()) {
				cameraPos.x = 0.5f*Gdx.graphics.getWidth();
			} else {
				core.setScreen(new MainMenu(core).postTransition());
			}
			break;
		default:
			break;
		}
	}

	@Override
	public Screen postTransition() {
		Image circle1 = new Image(core.getAssetManager().get("cybercircle1.png", Texture.class));
		circle1.setOrigin(circle1.getWidth()/2f, circle1.getHeight()/2f);
		circle1.setPosition(Gdx.graphics.getWidth(), circle1.getHeight()/3);
		stage.addActor(circle1);
		
		
		ms = new MusicSelectionPage(core);
		ms.miniSender.addListener(this);
		ms.beginMusicSearch();
		stage.addActor(ms);
		
		ap = new AnalyzePage(core.getDefaultSkin(), core.getAssetManager());
		ap.miniSender.addListener(this);
		ap.setPosition(Gdx.graphics.getWidth(), ap.getY());
		stage.addActor(ap);	
		return this;
	}
	
	public void setPhase(int phase) {
		cameraPos.x = Gdx.graphics.getWidth()*(phase + 0.5f);
	}
	
	@Override
	public void show() {
		Gdx.input.setInputProcessor(stage);
		super.show();
	}
	
	@Override
	public void dispose() {
		stage.dispose();
		super.dispose();
	}
}
