package zero1hd.polyjet.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
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.polyjet.Polyjet;
import zero1hd.polyjet.ui.pages.CreditsPage;
import zero1hd.polyjet.ui.pages.MainPage;
import zero1hd.polyjet.ui.pages.MoreOptionsPage;
import zero1hd.polyjet.ui.pages.OptionsPage;
import zero1hd.polyjet.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;
	
	Polyjet core;
	public MainMenu(final Polyjet core) {
		this.core = core;
		stage = new Stage(new ScreenViewport());
		targetPosition = new Vector3(stage.getCamera().position);
	}
	
	public Screen postTransition() {
		stage.clear();
		
		mainPage = new MainPage(core, targetPosition);
		mainPage.setPosition(0, 0);
		stage.addActor(mainPage);
		
		//End main menu
		moreOptionsPage = new MoreOptionsPage(core, targetPosition);
		
		optionsPage = new OptionsPage(core, targetPosition, moreOptionsPage);
		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.");
		return this;
	}
	
	@Override
	public void show() {
		Gdx.input.setInputProcessor(stage);
		super.show();
	}
	
	@Override
	public void hide() {
		if (optionsPage != null) {
			optionsPage.saveOptions(core.getPrefs());
		}
		super.hide();
	}
	
	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(1f, 1f, 1f, 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, 0.25f);
		}

		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;
		super.resize(width, height);
	}
	
	@Override
	public void dispose() {
		stage.dispose();
		super.dispose();
	}
}
