package com.eyeofmidas.cardtrick;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.eyeofmidas.cardtrick.InputScreen.DragDirection;

public class CardtrickGame extends Game {
	public static String TAG = "EyeOfMidas";

	private Stage stage;
	private AssetManager assetManager;

	private MainMenuScreen mainMenu;
	private LoadingScreen loading;
	private InputScreen input;
	private RevealScreen reveal;
	private TrainingIntroScreen trainingIntro;
	private TrainingScreen training;

	private ShapeRenderer shapeRenderer;

	@Override
	public void create() {
		stage = new Stage(new FitViewport(720, 1080));
		assetManager = new AssetManager();
		shapeRenderer = new ShapeRenderer();

		loading = new LoadingScreen();
		mainMenu = new MainMenuScreen();
		input = new InputScreen();
		reveal = new RevealScreen();
		trainingIntro = new TrainingIntroScreen();
		training = new TrainingScreen();
		goLoading();
	}

	@Override
	public void dispose() {
		stage.dispose();
		assetManager.dispose();
		shapeRenderer.dispose();

		loading.dispose();
		mainMenu.dispose();
		input.dispose();
		reveal.dispose();
		trainingIntro.dispose();
		training.dispose();
	}

	public Stage getStage() {
		return stage;
	}

	private void goLoading() {
		setScreen(loading);

	}

	public void goMainMenu() {
		setScreen(mainMenu);
	}

	public void goTrainingIntro() {
		setScreen(trainingIntro);
	}

	public void goTraining() {
		setScreen(training);
	}

	public void exit() {
		Gdx.app.exit();
	}

	public void goCredits() {
		// TODO Auto-generated method stub

	}

	public AssetManager getAssetManager() {
		return assetManager;
	}

	public ShapeRenderer getShapeRenderer() {
		return shapeRenderer;
	}

	public Skin getSkin() {
		Skin skin = new Skin();

		TextureAtlas imageAtlas = getAssetManager().get("data/uiskin.atlas", TextureAtlas.class);
		skin.add("logo", imageAtlas.createSprite("mindgames_logo"));
		skin.add("up", imageAtlas.createSprite("up_arrow"));
		skin.add("down", imageAtlas.createSprite("down_arrow"));
		skin.add("left", imageAtlas.createSprite("left_arrow"));
		skin.add("right", imageAtlas.createSprite("right_arrow"));
		skin.add("largeFont", getAssetManager().get("fonts/liberation-64-white.fnt", BitmapFont.class));
		skin.add("smallFont", getAssetManager().get("fonts/liberation-32-white.fnt", BitmapFont.class));

		TextButtonStyle textButtonStyle = new TextButtonStyle();
		textButtonStyle.font = skin.getFont("largeFont");
		textButtonStyle.fontColor = new Color(1, 1, 1, 1);
		textButtonStyle.overFontColor = new Color(0.95f, 0.63f, 0.24f, 1f);

		NinePatch button = imageAtlas.createPatch("button");

		textButtonStyle.up = new NinePatchDrawable(button);
		textButtonStyle.over = new NinePatchDrawable(button);
		textButtonStyle.down = new NinePatchDrawable(button);
		skin.add("default", textButtonStyle);

		LabelStyle labelStyle = new LabelStyle();
		labelStyle.font = skin.getFont("largeFont");
		skin.add("default", labelStyle);

		labelStyle = new LabelStyle();
		labelStyle.font = skin.getFont("smallFont");
		skin.add("small", labelStyle);

		skin.add("link", imageAtlas.createSprite("cardtrick_link"));
		return skin;
	}

	public void goInputScreen() {
		setScreen(input);
	}

	public void goReveal(Vector2 startTile, DragDirection dragDirection) {
		reveal.setTilePosition(startTile);
		reveal.setDragDirection(dragDirection);
		setScreen(reveal);

	}
}
