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

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

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

	@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(BONUS02);
				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) {
				GregorianCalendar calendar = new GregorianCalendar();
				int hour = calendar.get(GregorianCalendar.HOUR) + 2;
				
				boolean twelveTo3 = inBounds(event, 100, 140, 40, 40);
				boolean threeTo6 = inBounds(event, 90, 170, 40, 40);
				boolean sixTo9 = inBounds(event, 55, 170, 45, 40);
				boolean nineTo12 = inBounds(event, 55, 140, 45, 40);
				boolean breakfast = inBounds(event, 190, 180, 40, 40);
				boolean lunch = inBounds(event, 100, 270, 40, 40);
				boolean dinner = inBounds(event, 190, 270, 40, 40);

				// "out of clocks"
				if (!(inBounds(event, 55, 140, 80, 80) || inBounds(event, 190, 140, 80, 80) || 
						inBounds (event, 55, 270, 80, 80) || inBounds(event, 190, 270, 80, 80))) {
					if (Settings.isSoundEnabled())
						Assets.getOutOfBounds().play(1);
				}
				// current time
				else if (((hour >= 12) ||(hour <= 3)) && twelveTo3) {
					if (!checking[0]) {
						addRightAnswers(event.x, event.y);
						checking[0] = true;
					}
				} else if ((hour > 3) && (hour <= 6) && threeTo6) {
					if (!checking[0]) {
						addRightAnswers(event.x, event.y);
						checking[0] = true;
					}
				} else if ((hour > 6) && (hour <= 9) && sixTo9) {
					if (!checking[0]) {
						addRightAnswers(event.x, event.y);
						checking[0] = true;
					}
				} else if ((hour > 9) && (hour <= 12) && nineTo12) {
					if (!checking[0]) {
						addRightAnswers(event.x, event.y);
						checking[0] = true;
					}
				}

				// breakfast (supposed from 6 to 9)
				else if (breakfast) {
					if (!checking[1]) {
						addRightAnswers(event.x, event.y);
						checking[1] = true;
					}
				}
				// lunch (supposed from 12 to 3)
				else if (lunch) {
					if (!checking[2]) {
						addRightAnswers(event.x, event.y);
						checking[2] = true;
					}
				}
				// dinner (supposed from 9 to 12)
				else if (dinner) {
					if (!checking[3]) {
						addRightAnswers(event.x, event.y);
						checking[3] = true;
					}
				} 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 (inBounds(event, 120, 440, 80, 27)) {
					if (Settings.isSoundEnabled())
						Assets.getClick().play(1);
					game.setScreen(new Exercise03IntroScreen(game));
					return;
				}
			}
		}
	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.getExercise02game(), 0, 0);
		long timeNow = timeIni - System.currentTimeMillis();
		if (!aux) {
			g.drawPixmap(Assets.getTemps(), 235, g.getHeight() - 105);
			time = "" + timeNow / 1000;
			drawText(g, time, Assets.getNumbersDigital(), g.getWidth() 
					- time.length() * 10 - 50, g.getHeight() - 75);
		}
		if (state == GameState.GameOver) {
			drawGameOverUI();
			aux = true;
			if (wrongAnswers.size() == 0) {
				g.drawPixmap(Assets.getBonus110(), 230, 360);
				drawText(g, bonus, Assets.getNumbers2(), 230, 420);
			}
			else
				g.drawPixmap(Assets.getBravo(), 210, 360);
		}
		
		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() - 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, 440);
	}

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

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
	}

}