package com.uoc.educationalVG.memorycon;

import java.util.*;

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 Exercise07GameScreen extends GameScreen {

	GameState state = GameState.Ready;
	private boolean[] checking;
	private boolean aux;

	public Exercise07GameScreen(Game game) {
		super(game);
		this.oldScore = World.oldScore;
		this.score = World.score;
		this.checking = new boolean[14];
	}

	@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(checking)) {
			state = GameState.GameOver;
			if (wrongAnswers.size() == 0) {
				getBonus(BONUS07);
				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);
			boolean red = inBounds(event, 265, 110, 50, 50);
			boolean blue = inBounds(event, 145, 110, 50, 50);
			boolean green = inBounds(event, 25, 110, 50, 50);
			boolean yellow = inBounds(event, 205, 110, 50, 50);
			boolean purple = inBounds(event, 85, 110, 50, 50);

			boolean coffee = inBounds(event, 185, 250, 50, 50);
			boolean tomato = inBounds(event, 140, 250, 50, 50);
			boolean sky = inBounds(event, 90, 385, 50, 50);
			boolean pumpkin = inBounds(event, 185, 385, 50, 50);
			boolean lemon = inBounds(event, 185, 290, 50, 50);
			boolean coal = inBounds(event, 230, 385, 50, 50);
			boolean sea = inBounds(event, 50, 250, 50, 50);
			boolean grass1 = inBounds(event, 45, 290, 50, 50);
			boolean grass2 = inBounds(event, 45, 385, 50, 50);
			boolean gold = inBounds(event, 185, 340, 50, 50);

			if (event.type == TouchEvent.TOUCH_UP) {

				// out of bounds
				if (!(inBounds(event, 25, 105, 275, 40) || inBounds(event, 45,
						235, 225, 195))) {
					if (Settings.isSoundEnabled())
						Assets.getOutOfBounds().play(1);
				}

				else if (!checking[0]) {
					if (red) {
						addRightAnswers(event.x, event.y);
						checking[0] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else if (!checking[1]) {
					if (blue) {
						addRightAnswers(event.x, event.y);
						checking[1] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else if (!checking[2]) {
					if (green) {
						addRightAnswers(event.x, event.y);
						checking[2] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else if (!checking[3]) {
					if (yellow) {
						addRightAnswers(event.x, event.y);
						checking[3] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else if (!checking[4]) {
					if (purple) {
						addRightAnswers(event.x, event.y);
						checking[4] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else if (!checking[5]) {
					if (coffee) {
						addRightAnswers(event.x, event.y);
						checking[5] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else if (!checking[6]) {
					if (tomato) {
						addRightAnswers(event.x, event.y);
						checking[6] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else if (!checking[7]) {
					if (sky) {
						addRightAnswers(event.x, event.y);
						checking[7] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else if (!checking[8]) {
					if (pumpkin) {
						addRightAnswers(event.x, event.y);
						checking[8] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else if (!checking[9]) {
					if (lemon) {
						addRightAnswers(event.x, event.y);
						checking[9] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else if (!checking[10]) {
					if (coal) {
						addRightAnswers(event.x, event.y);
						checking[10] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else if (!checking[11]) {
					if (sea) {
						addRightAnswers(event.x, event.y);
						checking[11] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else if (!checking[12]) {
					if (grass1 || grass2) {
						addRightAnswers(event.x, event.y);
						checking[12] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else if (!checking[13]) {
					if (gold) {
						addRightAnswers(event.x, event.y);
						checking[13] = true;
					} else {
						addWrongAnswers(event.x, event.y);
					}
				} else {
					addWrongAnswers(event.x, event.y);
				}

			}
		}
	}

	private void updateGameOver(List<TouchEvent> touchEvents) {
		int len = touchEvents.size();
		Graphics g = game.getGraphics();
		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 (inBounds(event, g.getWidth() - 100, g.getHeight() - 30, 80,
						127)) {
					if (Settings.isSoundEnabled())
						Assets.getClick().play(1);
					game.setScreen(new FinalScoreScreen(game));
					return;
				}
			}
		}
	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.getExercise07game(), 0, 0);
		long timeNow = timeIni - System.currentTimeMillis();
		if (!aux) {
			g.drawPixmap(Assets.getTemps(), 235, g.getHeight() - 60);
			time = "" + timeNow / 1000;
			drawText(g, time, Assets.getNumbersDigital(),
					g.getWidth() - time.length() * 10 - 50, g.getHeight() - 35);
		}
		if (state == GameState.GameOver) {
			drawGameOverUI();
			aux = true;
			if (wrongAnswers.size() == 0) {
				g.drawPixmap(Assets.getBonus110(), 15, 380);
				drawText(g, bonus, Assets.getNumbers2(), 15, 440);
			} else
				g.drawPixmap(Assets.getBravo(), 5, 390);
		}
		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];
					drawRightAnswerUI(x - 5, y - 10);
				} 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() - 60);
		drawText(g, score, Assets.getNumbers(),
				g.getWidth() / 2 - score.length() * 20 / 2, g.getHeight() - 35);

	}

	private void drawGameOverUI() {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.getContinueButton(), g.getWidth() - 100,
				g.getHeight() - 30);
	}

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

	@Override
	public void pause() {
		Settings.addScore(World.oldScore);
		Settings.save(game.getFileIO());
	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {

	}

}