package zero1hd.polyjet.ui.pages;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
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 com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

import zero1hd.polyjet.Polyjet;
import zero1hd.polyjet.screens.PreGameScreen;

public class MainPage extends Page {
	private Image polyjetTitle;
	private Label polyJetVersion;
	private TextButton options;
	private Image cyberCircle;
	private Label begin;
	private TextButton quit;
	private TextButton credits;
	private WidgetGroup playButton;

	public MainPage(final Polyjet core, final Vector3 targetPosition) {
		polyjetTitle = new Image(core.getAssetManager().get("PolyjetTitle.png", Texture.class));
		polyjetTitle.setPosition(15, getHeight() - polyjetTitle.getHeight()-15);
		addActor(polyjetTitle);
		polyJetVersion = new Label("Version: " + Polyjet.VERSION, core.getDefaultSkin(), "sub-font",
				core.getDefaultSkin().getColor("default"));
		polyJetVersion.setPosition(3, 3);
		addActor(polyJetVersion);

		options = new TextButton(" Options ", core.getDefaultSkin(), "left");
		options.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				targetPosition.x = 1.5f * getWidth();
			}
		});
		options.setPosition(-options.getWidth(), polyjetTitle.getY() - options.getHeight() - 30);
		options.addAction(Actions.sequence(Actions.delay(0.25f), Actions.moveTo(0, options.getY(), 0.5f)));
		addActor(options);

		credits = new TextButton(" Credits ", core.getDefaultSkin(), "left");
		credits.addListener(new ChangeListener() {

			@Override
			public void changed(ChangeEvent event, Actor actor) {
				targetPosition.y = 1.5f * getHeight();
			}
		});
		credits.setPosition(-credits.getWidth(), options.getY() - credits.getHeight() - 10);
		credits.addAction(Actions.sequence(Actions.delay(0.5f), Actions.moveTo(0, credits.getY(), 0.5f)));
		addActor(credits);

		quit = new TextButton(" Quit ", core.getDefaultSkin(), "left");
		quit.setPosition(-quit.getWidth(), credits.getY() - credits.getHeight() - 10);
		quit.addListener(new ChangeListener() {

			@Override
			public void changed(ChangeEvent event, Actor actor) {
				Gdx.app.exit();
			}

		});
		quit.addAction(Actions.sequence(Actions.delay(0.75f), Actions.moveTo(0, quit.getY(), 0.5f)));
		addActor(quit);

		// begin play button
		playButton = new WidgetGroup();

		cyberCircle = new Image(core.getAssetManager().get("Tech-Circle1.png", Texture.class));
		cyberCircle.setOrigin(cyberCircle.getWidth() / 2, cyberCircle.getHeight() / 2);
		cyberCircle.setColor(0.7f, 0.7f, 0.7f, 0.8f);
		cyberCircle.addAction(Actions.forever(Actions.rotateBy(-360f, 10f)));
		playButton.addActor(cyberCircle);
		playButton.setSize(cyberCircle.getWidth(), cyberCircle.getHeight());

		begin = new Label("Play", core.getDefaultSkin(), "special-font", core.getDefaultSkin().getColor("default"));
		begin.setColor(1f, 1f, 1f, 1f);
		playButton.addActor(begin);
		begin.setPosition(((playButton.getWidth() - begin.getWidth()) / 2)-10,
				(playButton.getHeight() - begin.getHeight()) / 2);
		playButton.setPosition(getWidth() - cyberCircle.getWidth() * 3 / 4, -cyberCircle.getHeight() / 4);

		// begin animation of begin button
		playButton.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (targetPosition.x == 0.5f * getWidth()) {
					cyberCircle.addAction(
							Actions.sequence(Actions.parallel(Actions.scaleBy(2f, 2f, 0.25f), Actions.fadeOut(0.25f)),
									Actions.run(new Runnable() {
										@Override
										public void run() {
											core.setScreen(new PreGameScreen(core));
										}
									}), Actions.parallel(Actions.scaleTo(1, 1), Actions.alpha(0.6f))));
				}
				super.clicked(event, x, y);
			}
		});
		addActor(playButton);
		// end play button
	}
	
}
