package com.uoc.educationalVG.memorycon;

import java.util.List;

import android.graphics.Color;

import com.uoc.educationalVG.framework.Game;
import com.uoc.educationalVG.framework.Graphics;
import com.uoc.educationalVG.framework.Input.TouchEvent;
import com.uoc.educationalVG.utils.Assets;
import com.uoc.educationalVG.utils.Settings;
import com.uoc.educationalVG.utils.World;

/**
 * @author "Beginning Android 4 Games Development"
 * @author (adapted by Sergi Colina)
 * 
 */
public class Exercise05GameScreen extends GameScreen {

	GameState state = GameState.Ready;
	boolean[] checking, checkAnswers;
	boolean aux;

	public Exercise05GameScreen(Game game) {
		super(game);
		this.oldScore = World.oldScore;
		this.score = World.score;
		this.checking = new boolean[8]; // hand, glove, ear, earring, rain,
										// cloud, pyramid, sphinx
		this.checkAnswers = new boolean[4]; // whether couples are correct or
											// not
	}

	@Override
	public void update(float deltaTime) {
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();

		if (state == GameState.Ready) {
			timeIni = System.currentTimeMillis();
			state = GameState.Running;
		}
		if (state == GameState.Running)
			updateRunning(touchEvents, deltaTime);
		if (state == GameState.GameOver)
			updateGameOver(touchEvents);
	}

	public void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {
		int len = touchEvents.size();
		if (areAllTrue(checkAnswers)) {
			state = GameState.GameOver;
			if (wrongAnswers.size() == 0) {
				getBonus(BONUS05);
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if (Settings.isSoundEnabled())
					Assets.getGameOver().play(1);

			} else {
				if (Settings.isSoundEnabled())
					Assets.getGameOver().play(1);
			}
		}
		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_DOWN) {
				// hand & glove
				if (inBounds(event, 70, 120, 40, 60)) // first: hand
					goChecking(0);
				else if (inBounds(event, 30, 180, 40, 50)) // first: glove
					goChecking(1);

				// ear & earring
				else if (inBounds(event, 70, 260, 50, 60)) // first: ear
					goChecking(2);
				else if (inBounds(event, 30, 315, 40, 50)) // first: earring
					goChecking(3);

				// rain & cloud
				else if (inBounds(event, 210, 120, 50, 50)) // first: rain
					goChecking(4);
				else if (inBounds(event, 230, 190, 60, 30)) // first: cloud
					goChecking(5);

				// pyramid & sphinx
				else if (inBounds(event, 200, 250, 55, 50)) // first:
															// pyramid
					goChecking(6);
				else if (inBounds(event, 240, 295, 50, 60)) // first: sphinx
					goChecking(7);

				else {
					if (Settings.isSoundEnabled())
						Assets.getWrong().play(1);
				}
			}

			else if (event.type == TouchEvent.TOUCH_DRAGGED) {
				addCoordinates(event.x, event.y);
			}

			else if (event.type == TouchEvent.TOUCH_UP) {
				if (checking[0]) { // first: hand
					if (inBounds(event, 30, 180, 40, 50)) { // next: glove
						incScore(0);
					} else {
						checking[0] = false;
						addWrongAnswers(event.x, event.y);
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				}
				if (checking[1]) { // first: glove
					if (inBounds(event, 70, 120, 40, 60)) { // next: hand
						incScore(0);
					} else {
						checking[1] = false;
						addWrongAnswers(event.x, event.y);
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				}
				if (checking[2]) { // first: ear
					if (inBounds(event, 30, 315, 40, 50)) { // next: earring
						incScore(1);
					} else {
						checking[2] = false;
						addWrongAnswers(event.x, event.y);
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				}
				if (checking[3]) { // first: earring
					if (inBounds(event, 70, 260, 50, 60)) { // next: ear
						incScore(1);
					} else {
						checking[3] = false;
						addWrongAnswers(event.x, event.y);
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				}
				if (checking[4]) { // first: rain
					if (inBounds(event, 230, 190, 60, 30)) { // next: cloud
						incScore(2);
					} else {
						checking[4] = false;
						addWrongAnswers(event.x, event.y);
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				}
				if (checking[5]) { // first: cloud
					if (inBounds(event, 210, 120, 50, 50)) { // next: rain
						incScore(2);
					} else {
						checking[5] = false;
						addWrongAnswers(event.x, event.y);
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				}
				if (checking[6]) { // first: pyramid
					if (inBounds(event, 240, 295, 50, 60)) { // next: sphinx
						incScore(3);
					} else {
						checking[6] = false;
						addWrongAnswers(event.x, event.y);
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				}
				if (checking[7]) { // first: sphinx
					if (inBounds(event, 200, 250, 55, 50)) { // next: pyramid
						incScore(3);
					} else {
						checking[7] = false;
						addWrongAnswers(event.x, event.y);
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				}
			}

			else {
				if (Settings.isSoundEnabled())
					Assets.getOutOfBounds().play(1);
			}
		}
	}

	private void updateGameOver(List<TouchEvent> touchEvents) {
		int len = touchEvents.size();
		World.oldScore = oldScore + oldBonus;
		World.score = "" + World.oldScore;

		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				if (event.x >= 120 && event.x <= 200 && event.y >= 440
						&& event.y <= 467) {
					if (Settings.isSoundEnabled())
						Assets.getClick().play(1);
					game.setScreen(new Exercise06GameScreen(game));
					return;
				}
			}
		}
	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.getExercise05game(), 0, 0);
		long timeNow = timeIni - System.currentTimeMillis();
		if (!aux) {
			g.drawPixmap(Assets.getTemps(), 235, g.getHeight() - 95);
			time = "" + timeNow / 1000;
			drawText(g, time, Assets.getNumbersDigital(),
					g.getWidth() - time.length() * 10 - 50, g.getHeight() - 65);
		}

		if (state == GameState.GameOver) {
			drawGameOverUI();
			aux = true;
			if (wrongAnswers.size() == 0) {
				g.drawPixmap(Assets.getBonus110(), 230, 360);
				drawText(g, bonus, Assets.getNumbers2(), 235, 420);
			} else
				g.drawPixmap(Assets.getBravo(), 215, 360);
		}

		if (checkAnswers[0]) // hand && glove
			drawRightAnswerUI(30, 120);
		if (checkAnswers[1]) // ear && earring
			drawRightAnswerUI(30, 250);
		if (checkAnswers[2]) // rain && cloud
			drawRightAnswerUI(170, 120);
		if (checkAnswers[3]) // sphinx && pyramid
			drawRightAnswerUI(170, 250);
		if (rightAnswers.size() > 0) {
			for (int i = 0; i < rightAnswers.size(); i++) {
				try {
					Integer[] coordinates = rightAnswers.get(i);
					int x = coordinates[0];
					int y = coordinates[1];
					drawPixelUI(x, y);
				} catch (Exception e) {
					throw new RuntimeException("Couldn't load coordinates");
				}
			}
		}

		if (wrongAnswers.size() > 0) {
			for (int i = 0; i < wrongAnswers.size(); i++) {
				Integer[] coordinates = wrongAnswers.get(i);
				int x = coordinates[0];
				int y = coordinates[1];
				drawWrongAnswerUI(x - 5, y - 10);
			}
		}

		g.drawPixmap(Assets.getPuntuacio(), g.getWidth() / 2
				- Assets.getPuntuacio().getWidth() / 2, g.getHeight() - 110);
		drawText(g, score, Assets.getNumbers(),
				g.getWidth() / 2 - score.length() * 20 / 2, g.getHeight() - 80);

	}

	private void drawGameOverUI() {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.getContinueButton(), 120, 445);
	}

	private void drawPixelUI(int x, int y) {
		Graphics g = game.getGraphics();
		for (int j = 0; j < 8; j++) {
			g.drawPixel(x + 2, y + j, Color.CYAN);
		}
	}

	public void drawRightAnswerUI(int x, int y) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.getOk110(), x, y);
	}

	public void addCoordinates(int x, int y) {
		Integer[] okCoordinates = new Integer[2];
		this.rightAnswers.add(okCoordinates);
		okCoordinates[0] = x;
		okCoordinates[1] = y;
	}

	public static boolean areAllTrue(boolean[] array) {
		for (boolean b : array)
			if (!b)
				return false;
		return true;
	}

	/**
	 * check if the image selection is right
	 * 
	 * @param i
	 */
	private void goChecking(int i) {
		if (!checking[i]) {
			if (Settings.isSoundEnabled())
				Assets.getClick().play(1);
			checking[i] = true;
		} else {
			if (Settings.isSoundEnabled())
				Assets.getWrong().play(1);
			checking[i] = false;
		}
	}

	/**
	 * increase score and set answers to true
	 * 
	 * @param one
	 *            of the 4 quadrants (from 0 to 3)
	 */
	private void incScore(int i) {
		oldScore += 10;
		score = "" + oldScore;
		checkAnswers[i] = true;
		if (Settings.isSoundEnabled()) {
			Assets.getCorrect().play(1);
		}
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {

	}

}