package zero1hd.rhythmbullet.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.ScreenViewport;

import zero1hd.rhythmbullet.RhythmBullet;
import zero1hd.rhythmbullet.audio.SongController;
import zero1hd.rhythmbullet.audio.SongList;
import zero1hd.rhythmbullet.ui.pages.CreditsPage;
import zero1hd.rhythmbullet.ui.pages.MainPage;
import zero1hd.rhythmbullet.ui.pages.MoreOptionsPage;
import zero1hd.rhythmbullet.ui.pages.OptionsPage;
import zero1hd.rhythmbullet.util.TransitionAdapter;

public class MainMenu extends ScreenAdapter implements TransitionAdapter {
	public Stage stage;
	private Vector3 targetPosition;
	
	private MainPage mainPage;
	private OptionsPage optionsPage;
	private CreditsPage creditsPage;
	private MoreOptionsPage moreOptionsPage;
	
	private RhythmBullet core;
	
	private SongController sc;
	private float lerpAlpha;
	public MainMenu(final RhythmBullet core) {
		this.core = core;
		stage = new Stage(new ScreenViewport());
		targetPosition = new Vector3(stage.getCamera().position);
		
		SongList songList = new SongList();
		songList.setSearchPath(core.getPrefs().getString("music dir"));
		songList.refresh();
		
		sc = new SongController(songList, core.getPrefs());
		sc.setAutoPlay(true);
		sc.setShuffle(true);
		sc.play();
		
		postTransition();
	}
	
	@Override
	public void preTransition() {
		stage.clear();
		mainPage.dispose();
		optionsPage.dispose();
		creditsPage.dispose();
		moreOptionsPage.dispose();
	}
	
	@Override
	public void postTransition() {
		mainPage = new MainPage(core, targetPosition, sc);
		mainPage.setPosition(0, 0);
		stage.addActor(mainPage);
		
		//End main menu
		moreOptionsPage = new MoreOptionsPage(core, targetPosition);
		
		optionsPage = new OptionsPage(core, targetPosition, moreOptionsPage, sc);
		optionsPage.setPosition(Gdx.graphics.getWidth(), 0);
		stage.addActor(optionsPage);
		
		creditsPage = new CreditsPage(core.getDefaultSkin());
		creditsPage.setPosition(0, Gdx.graphics.getHeight());
		stage.addActor(creditsPage);
		
		moreOptionsPage.setPosition(1f*Gdx.graphics.getWidth(), -1f*Gdx.graphics.getHeight());
		stage.addActor(moreOptionsPage);
		
		stage.addListener(new InputListener() {
			@Override
			public boolean keyUp(InputEvent event, int keycode) {
				if (keycode == Keys.ESCAPE) {
					stage.unfocusAll();
					if (targetPosition.x != 0.5f*Gdx.graphics.getWidth() || targetPosition.y != 0.5f*Gdx.graphics.getHeight()) {
						targetPosition.x = 0.5f*Gdx.graphics.getWidth();
						targetPosition.y = 0.5f*Gdx.graphics.getHeight();
					}
					moreOptionsPage.controlUnselect();
				}
				return super.keyUp(event, keycode);
			}
		});	
		
		stage.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (stage.hit(x, y, true) == null) {
					stage.unfocusAll();
					moreOptionsPage.controlUnselect();
				}
				super.clicked(event, x, y);
			}
		});
		
		Gdx.app.debug("Post Transition", "Beginning screen setup for Main menu.");
	}
	
	@Override
	public void show() {
		Gdx.input.setInputProcessor(stage);
		calcLerpAlpha(Gdx.graphics.getWidth());
		super.show();
	}
	
	@Override
	public void hide() {
		saveAll();
		super.hide();
	}
	
	public void saveAll() {
		if (optionsPage != null) {
			optionsPage.saveOptions(core.getPrefs());
			moreOptionsPage.getGraphicsSettings().save(core.getPrefs());
			core.getPrefs().flush();
		}
	}
	
	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		stage.act();
		stage.draw();
		if (stage.getCamera().position.x != targetPosition.x || stage.getCamera().position.y != targetPosition.y) {
			stage.getCamera().position.lerp(targetPosition, lerpAlpha);
		}
		
		sc.getCurrentSong().playbackIndexUpdate();
		
		super.render(delta);
	}
	
	@Override
	public void resize(int width, int height) {
		stage.getViewport().update(width, height, false);
		targetPosition.x = width/2;
		targetPosition.y = height/2;
		calcLerpAlpha(width);
		super.resize(width, height);
	}
	
	@Override
	public void dispose() {
		stage.dispose();
		super.dispose();
	}
	
	private void calcLerpAlpha(int width) {
		if (width >= 3835) {
			lerpAlpha = 0.005f;
		} else {
			lerpAlpha = 0.25f;
		}
	}
}
