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

	GameState state = GameState.Ready;
	private boolean aux;

	public Exercise06GameScreen(Game game) {
		super(game);
		this.oldScore = World.oldScore;
		this.score = World.score;
	}

	@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();
		
		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				// out of bounds
				if (!(inBounds(event, 50, 160, 100, 110)
						|| inBounds(event, 170, 160, 100, 100)
						|| inBounds(event, 70, 300, 70, 90)
						|| inBounds(event, 190, 300, 80, 110))) {
					if (Settings.isSoundEnabled())
						Assets.getOutOfBounds().play(1);
				}
				else if (inBounds(event, 70, 300, 70, 90)) {	//apple
					if (Settings.isSoundEnabled())
						Assets.getCorrect().play(1);
					
					oldScore += 10;
					score = "" + oldScore;
					state = GameState.GameOver;
					if (Settings.isSoundEnabled())
						Assets.getGameOver().play(1);
					
					if (wrongAnswers.size() == 0) 
						getBonus(BONUS06);
					
				}
				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, 450, 80, 27)) {
					if (Settings.isSoundEnabled())
						Assets.getClick().play(1);
					game.setScreen(new Exercise07IntroScreen(game));
					return;
				}
			}
		}
	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
        g.drawPixmap(Assets.getBackground(), 0, 0);
        g.drawPixmap(Assets.getExercise06game0(), 0, 0);
        long timeNow = timeIni - System.currentTimeMillis();
		if (!aux) {
			g.drawPixmap(Assets.getTemps(), 235, g.getHeight() - 85);
			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, 360);
				drawText(g, bonus, Assets.getNumbers2(), 230, 420);
			}
			else
				g.drawPixmap(Assets.getBravo(), 220, 380);
		}
		
		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() - 90);
		drawText(g, score, Assets.getNumbers(), g.getWidth() / 2 - score.length() * 20 / 2,
				g.getHeight() - 63);
	}

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

	@Override
	public void pause() {
	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {

	}

}