package zero1hd.rhythmbullet.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.ScreenViewport;

import zero1hd.rhythmbullet.RhythmBullet;
import zero1hd.rhythmbullet.audio.SongList;
import zero1hd.rhythmbullet.ui.pages.AnalyzePage;
import zero1hd.rhythmbullet.ui.pages.MusicSelectionPage;
import zero1hd.rhythmbullet.util.MiniEvents;
import zero1hd.rhythmbullet.util.MiniListener;
import zero1hd.rhythmbullet.util.TransitionAdapter;

@Deprecated
public class PreGameScreen extends ScreenAdapter implements TransitionAdapter, MiniListener {
	Stage stage;
	
	MusicSelectionPage ms;
	public AnalyzePage ap;
	private Vector3 cameraPos;
	private RhythmBullet core;
	private SongList songList;
	
	public PreGameScreen(RhythmBullet core, SongList songList) {
		stage = new Stage(new ScreenViewport());
		cameraPos = new Vector3(stage.getCamera().position);
		this.core = core;
		this.songList = songList;
		postTransition();
	}
	
	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        
        stage.getViewport().apply();
        try {
            stage.act();
            stage.draw();
        } catch (NullPointerException e) {
        	e.printStackTrace();
        }
        
		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, true);
		cameraPos.set(stage.getCamera().position);
		super.resize(width, height);
	}
	
	@Override
	public void handle(MiniEvents ID) {
		switch (ID) {
		case MUSIC_SELECTED:
			cameraPos.x = 1.5f*Gdx.graphics.getWidth();
			ap.setSong(songList.getAudioData(ms.getSelectedMusic()), ms.getSelectedMusicInfo(), this);
			break;
		case BACK:
			if (cameraPos.x == 1.5f*Gdx.graphics.getWidth()) {
				cameraPos.x = 0.5f*Gdx.graphics.getWidth();
			} else {
				core.setScreen(new MainMenu(core));
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public void preTransition() {
		stage.clear();
		ap.dispose();
		ms.dispose();
	}
	
	@Override
	public void postTransition() {
		ms = new MusicSelectionPage(core.getDefaultSkin(), songList, core.getAssetManager());
		ms.miniSender.addListener(this);
		stage.addActor(ms);
		
		ap = new AnalyzePage(core);
		ap.miniSender.addListener(this);
		ap.setPosition(Gdx.graphics.getWidth(), 0);
		stage.addActor(ap);	
	}
	
	@Override
	public void show() {
		Gdx.input.setInputProcessor(stage);
		super.show();
	}
	
	@Override
	public void dispose() {
		stage.dispose();
		super.dispose();
	}
}
