package com.eyeofmidas.cardtrick;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.utils.Scaling;
import com.eyeofmidas.cardtrick.InputScreen.DragDirection;

public class RevealScreen implements Screen {

	private CardtrickGame game;
	private TextureAtlas cardAtlas;
	private DragDirection direction;
	private Vector2 position;
	private float alpha;
	private Image cardImage;

	public RevealScreen() {
		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().clear();
		cardAtlas = game.getAssetManager().get("data/cards.atlas", TextureAtlas.class);
		alpha = 0.01f;
		String cardName = (String) CardTranslator.translate(position, direction);
		String cleanedCardName = cardName.toLowerCase().replaceAll(" ", "-");
		Sprite cardSprite = cardAtlas.createSprite(cleanedCardName);
		cardSprite.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
		cardImage = new Image(cardSprite);
		cardImage.setSize(game.getStage().getWidth() * 0.75f, game.getStage().getHeight() * 0.75f);
		cardImage.setScaling(Scaling.fit);
		cardImage.setAlign(Align.center);
		cardImage.setPosition((game.getStage().getWidth() - cardImage.getWidth()) / 2, (game.getStage().getHeight() - cardImage.getHeight()) / 2);
		cardImage.setColor(1f, 1f, 1f, 0f);

		game.getStage().addActor(cardImage);

		game.getStage().addListener(new InputListener() {
			@Override
			public boolean keyDown(InputEvent event, int keycode) {
				switch (keycode) {
				case Input.Keys.BACK:
				case Input.Keys.ESCAPE:
					game.goMainMenu();
					break;
				}
				return false;
			}

			@Override
			public boolean touchDown(InputEvent event, float screenX, float screenY, int pointer, int button) {
				return true;
			}

			@Override
			public void touchDragged(InputEvent event, float x, float y, int pointer) {
				rub((int) x, (int) y);
			}
		});

		Gdx.input.setInputProcessor(game.getStage());
		Gdx.input.setCatchBackKey(true);
	}

	@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

	}

	public void setTilePosition(Vector2 tilePos) {
		position = tilePos;
	}

	public void setDragDirection(DragDirection dragDir) {
		direction = dragDir;
	}

	public void rub(int screenX, int screenY) {
		// Gdx.app.log("EyeOfMidas", "rubbing");
		alpha *= 1.05f;
		alpha = MathUtils.clamp(alpha, 0, 1);
		Color curColor = cardImage.getColor();
		curColor.a = alpha;
		cardImage.setColor(curColor);
	}

}
