/* stars */

package com.uoc.educationalVG.memorycon;

import java.util.List;

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

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

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

	@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 (found >= 18) {
			state = GameState.GameOver;
			if (wrongAnswers.size() == 0) {
				getBonus(BONUS04B);
				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_UP) {
				if (!(inBounds(event, 40, 110, 250, 280))) { // out of bounds
					if (Settings.isSoundEnabled())
						Assets.getOutOfBounds().play(1);
				} else if (inBounds(event, 40, 115, 40, 40)) { // i=1 j=1
					if (!checking[0]) {
						addCoordinates(event.x, event.y);
						checking[0] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 145, 115, 40, 40)) { // i=1 j=4
					if (!checking[1]) {
						addCoordinates(event.x, event.y);
						checking[1] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 225, 115, 40, 40)) { // i=1 j=6
					if (!checking[2]) {
						addCoordinates(event.x, event.y);
						checking[2] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 145, 155, 40, 40)) { // i=2 j=4
					if (!checking[3]) {
						addCoordinates(event.x, event.y);
						checking[3] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 145, 190, 40, 40)) { // i=3 j=4
					if (!checking[4]) {
						addCoordinates(event.x, event.y);
						checking[4] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 180, 190, 40, 40)) { // i=3 j=5
					if (!checking[5]) {
						addCoordinates(event.x, event.y);
						checking[5] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 40, 225, 40, 40)) { // i=4 j=1
					if (!checking[6]) {
						addCoordinates(event.x, event.y);
						checking[6] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 145, 225, 40, 40)) { // i=4 j=4
					if (!checking[7]) {
						addCoordinates(event.x, event.y);
						checking[7] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 225, 225, 40, 40)) { // i=4 j=6
					if (!checking[8]) {
						addCoordinates(event.x, event.y);
						checking[8] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 40, 260, 40, 40)) { // i=5 j=1
					if (!checking[9]) {
						addCoordinates(event.x, event.y);
						checking[9] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 180, 260, 40, 40)) { // i=5 j=5
					if (!checking[10]) {
						addCoordinates(event.x, event.y);
						checking[10] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 225, 260, 40, 40)) { // i=5 j=6
					if (!checking[11]) {
						addCoordinates(event.x, event.y);
						checking[11] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 40, 295, 40, 40)) { // i=6 j=1
					if (!checking[12]) {
						addCoordinates(event.x, event.y);
						checking[12] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 145, 295, 40, 40)) { // i=6 j=4
					if (!checking[13]) {
						addCoordinates(event.x, event.y);
						checking[13] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 225, 295, 40, 40)) { // i=6 j=6
					if (!checking[14]) {
						addCoordinates(event.x, event.y);
						checking[14] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 145, 330, 40, 40)) { // i=7 j=4
					if (!checking[15]) {
						addCoordinates(event.x, event.y);
						checking[15] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 145, 365, 40, 40)) { // i=8 j=4
					if (!checking[16]) {
						addCoordinates(event.x, event.y);
						checking[16] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else if (inBounds(event, 180, 365, 40, 40)) { // i=8 j=5
					if (!checking[17]) {
						addCoordinates(event.x, event.y);
						checking[17] = true;
					} else {
						if (Settings.isSoundEnabled())
							Assets.getWrong().play(1);
					}
				} else {
					addWrongAnswers(event.x, event.y);
				}
			}
		}
	}

	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 >= 450
						&& event.y <= 467) {
					if (Settings.isSoundEnabled())
						Assets.getClick().play(1);
					game.setScreen(new Exercise05IntroScreen(game));
					return;
				}
			}
		}
	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.getExercise04bgame(), 0, 0);
		long timeNow = timeIni - System.currentTimeMillis();
		if (!aux) {
			g.drawPixmap(Assets.getTemps(), 235, g.getHeight() - 80);
			time = "" + timeNow / 1000;
			drawText(g, time, Assets.getNumbersDigital(),
					g.getWidth() - time.length() * 10 - 50, g.getHeight() - 55);
		}
		if (state == GameState.GameOver) {
			drawGameOverUI();
			aux = true;
			if (wrongAnswers.size() == 0) {
				g.drawPixmap(Assets.getBonus110(), 230, 375);
				drawText(g, bonus, Assets.getNumbers2(), 230, 420);
			} else
				g.drawPixmap(Assets.getBravo(), 220, 385);
		}

		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 - 10, 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);

			}
		}

		drawText(g, "" + found, Assets.getNumbers(), g.getWidth() - 60,
				g.getHeight() - 415);
		g.drawPixmap(Assets.getPuntuacio(), g.getWidth() / 2
				- Assets.getPuntuacio().getWidth() / 2, g.getHeight() - 85);
		drawText(g, score, Assets.getNumbers(),
				g.getWidth() / 2 - score.length() * 20 / 2, g.getHeight() - 60);

	}

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

	public void addCoordinates(int x, int y) {
		this.found++;
		Integer[] okCoordinates = new Integer[2];
		this.rightAnswers.add(okCoordinates);
		okCoordinates[0] = x;
		okCoordinates[1] = y;
		oldScore += 10;
		score = "" + oldScore;
		if (Settings.isSoundEnabled())
			Assets.getStar().play(1);
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {

	}

}