package com.uoc.educationalVG.memorycon;

import java.util.Calendar;
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 Exercise01GameScreen extends GameScreen {

	GameState state = GameState.Ready;
	private World world;
	private boolean dayCheck, monthCheck, seasonCheck;
	private boolean aux;

	public Exercise01GameScreen(Game game) {
		super(game);
		this.world = new World();
		this.oldScore = 0;
		this.score = "0";
	}

	@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();

		/*
		 * Calculate day, month, season
		 */
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		int month = calendar.get(Calendar.MONTH);
		String season = getSeason();

		if ((dayCheck) && (monthCheck) && (seasonCheck)) {
			state = GameState.GameOver;
			if (wrongAnswers.size() == 0) {
				getBonus(BONUS01);
				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 dilluns = inBounds(event, 30, 40, 70, 20);
			boolean dimarts = inBounds(event, 110, 40, 70, 20);
			boolean dimecres = inBounds(event, 190, 40, 70, 20);
			boolean dijous = inBounds(event, 30, 70, 70, 20);
			boolean divendres = inBounds(event, 110, 70, 70, 20);
			boolean dissabte = inBounds(event, 190, 70, 70, 20);
			boolean diumenge = inBounds(event, 30, 100, 70, 20);

			boolean gener = inBounds(event, 30, 150, 70, 20);
			boolean febrer = inBounds(event, 110, 150, 70, 20);
			boolean març = inBounds(event, 190, 150, 70, 20);
			boolean abril = inBounds(event, 30, 180, 70, 20);
			boolean maig = inBounds(event, 110, 180, 70, 20);
			boolean juny = inBounds(event, 190, 180, 70, 20);
			boolean juliol = inBounds(event, 30, 210, 70, 20);
			boolean agost = inBounds(event, 110, 210, 70, 20);
			boolean setembre = inBounds(event, 190, 210, 70, 20);
			boolean octubre = inBounds(event, 30, 240, 70, 20);
			boolean novembre = inBounds(event, 110, 240, 70, 20);
			boolean desembre = inBounds(event, 190, 240, 70, 20);

			boolean primavera = inBounds(event, 30, 290, 70, 20);
			boolean estiu = inBounds(event, 110, 290, 70, 20);
			boolean tardor = inBounds(event, 190, 290, 70, 20);
			boolean hivern = inBounds(event, 30, 320, 70, 20);

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

				// out of bounds
				if (!(dilluns || dimarts || dimecres || dijous || divendres
						|| dissabte || diumenge || gener || febrer || març
						|| abril || maig || juny || juliol || agost || setembre
						|| octubre || novembre || desembre || primavera
						|| estiu || tardor || hivern)) {
					if (Settings.isSoundEnabled())
						Assets.getOutOfBounds().play(1);
				}

				// day of week
				else if (((day == 1) && diumenge) || ((day == 2) && dilluns)
						|| ((day == 3) && dimarts) || ((day == 4) && dimecres)
						|| ((day == 5) && dijous) || ((day == 6) && divendres)
						|| ((day == 7) && dissabte)) {
					if (!dayCheck) {
						addRightAnswers(event.x, event.y);
						dayCheck = true;
					}
				}

				// month
				else if (((month == 0) && gener) || ((month == 1) && febrer)
						|| ((month == 2) && març) || ((month == 3) && abril)
						|| ((month == 4) && maig) || ((month == 5) && juny)
						|| ((month == 6) && juliol) || ((month == 7) && agost)
						|| ((month == 8) && setembre)
						|| ((month == 9) && octubre)
						|| ((month == 10) && novembre)
						|| ((month == 11) && desembre)) {
					if (!monthCheck) {
						addRightAnswers(event.x, event.y);
						monthCheck = true;
					}
				}

				// season
				else if (((season == "Primavera") && primavera)
						|| ((season == "Estiu") && estiu)
						|| ((season == "Tardor") && tardor)
						|| ((season == "Hivern") && hivern)) {
					if (!seasonCheck) {
						addRightAnswers(event.x, event.y);
						seasonCheck = 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 (event.x >= 120 && event.x <= 200 && event.y >= 440
						&& event.y <= 467) {
					if (Settings.isSoundEnabled())
						Assets.getClick().play(1);
					game.setScreen(new Exercise02IntroScreen(game));
					return;
				}
			}
		}
	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.getExercise01game(), 0, 0);
		long timeNow = timeIni - System.currentTimeMillis();
		if (!aux) {
			g.drawPixmap(Assets.getTemps(), 235, g.getHeight() - 115);
			time = "" + timeNow / 1000;
			drawText(g, time, Assets.getNumbersDigital(),
					g.getWidth() - time.length() * 10 - 50, g.getHeight() - 85);
		}
		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 (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() - 120);
		drawText(g, score, Assets.getNumbers(),
				g.getWidth() / 2 - score.length() * 20 / 2, g.getHeight() - 85);
	}

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

	/**
	 * Calculates the season of the year
	 * @return the season
	 */
	private String getSeason() {
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int month = calendar.get(Calendar.MONTH);
		String season = "";
		if ((month == 0) || (month == 1))
			season = "Hivern";
		else if ((month == 3) || (month == 4))
			season = "Primavera";
		else if ((month == 6) || (month == 7))
			season = "Estiu";
		else if ((month == 9) || (month == 1))
			season = "Tardor";
		else if ((month == 2) && (day <= 19))
			season = "Hivern";
		else if ((month == 2) && (day >= 20))
			season = "Primavera";
		else if ((month == 5) && (day <= 20))
			season = "Primavera";
		else if ((month == 5) && (day >= 21))
			season = "Estiu";
		else if ((month == 8) && (day <= 20))
			season = "Estiu";
		else if ((month == 8) && (day >= 21))
			season = "Tardor";
		else if ((month == 11) && (day <= 21))
			season = "Tardor";
		else if ((month == 11) && (day >= 22))
			season = "Hivern";

		return season;

	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
	}

	public World getWorld() {
		return world;
	}

	public void setWorld(World world) {
		this.world = world;
	}

}