package com.eyeofmidas.cardtrick;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
import com.badlogic.gdx.utils.Scaling;

public class MainMenuScreen implements Screen {

	private CardtrickGame game;

	public MainMenuScreen() {
		game = (CardtrickGame) Gdx.app.getApplicationListener();
	}

	@Override
	public void render(float delta) {
		game.getStage().act(delta);

		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		game.getStage().draw();
	}

	@Override
	public void resize(int width, int height) {
		game.getStage().getViewport().update(width, height, true);
	}

	@Override
	public void show() {
		// game.getStage().addAction(Actions.color(Color.BLACK));
		// game.getStage().addAction(Actions.alpha(0f));

		Gdx.input.setInputProcessor(game.getStage());
		Skin skin = game.getSkin();

		Table root = new Table();

		root.setBackground(new SpriteDrawable(new Sprite(game.getAssetManager().get("wallpaper.png", Texture.class))));
		root.setFillParent(true);
		root.center();

		Image logo = new Image(skin.getDrawable("logo"));
		logo.setScaling(Scaling.fit);

		root.add(logo).pad(0, 100, 0, 100).top();

		root.row().colspan(2);

		Button startButton = new TextButton("Start", skin);
		startButton.addListener(new InputListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				return true;
			}

			@Override
			public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
				game.goInputScreen();
			}
		});
		startButton.pad(10);
		root.add(startButton);
		root.row().colspan(2).space(10);

		Button trainingButton = new TextButton("Training", skin);
		trainingButton.addListener(new InputListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				return true;
			}

			@Override
			public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
				game.goTrainingIntro();
			}
		});
		trainingButton.pad(10);
		root.add(trainingButton);
		root.row().colspan(2).space(10);

		Button exitButton = new TextButton("Exit", skin);
		exitButton.addListener(new InputListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				return true;
			}

			@Override
			public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
				game.exit();
			}
		});
		exitButton.pad(20);
		root.add(exitButton);
		root.row();
		game.getStage().addActor(root);
		game.getStage().addListener(new InputListener() {
			@Override
			public boolean keyDown(InputEvent event, int keyCode) {
				switch (keyCode) {
				case Input.Keys.BACK:
				case Input.Keys.ESCAPE:
					game.exit();
					break;
				}
				return false;
			}
		});

		Gdx.input.setCatchBackKey(false);

		// game.getStage().addAction(Actions.fadeIn(0.5f));
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
