package com.uoc.educationalVG.memorycon;

import java.util.ArrayList;

import com.uoc.educationalVG.framework.Game;
import com.uoc.educationalVG.framework.Graphics;
import com.uoc.educationalVG.framework.Pixmap;
import com.uoc.educationalVG.framework.Screen;
import com.uoc.educationalVG.framework.Input.TouchEvent;
import com.uoc.educationalVG.utils.Assets;
import com.uoc.educationalVG.utils.Settings;

/**
 * A superclass for all our Game Screens
 * @author "Beginning Android 4 Games Development"
 * @author (adapted by Sergi Colina)
 *
 */
public class GameScreen extends Screen {

	protected enum GameState {
		Ready, Running, Paused, GameOver
	}

	protected ArrayList<Integer[]> rightAnswers;
	protected ArrayList<Integer[]> wrongAnswers;

	public GameScreen(Game game) {
		super(game);
		this.rightAnswers = new ArrayList<Integer[]>();
		this.wrongAnswers = new ArrayList<Integer[]>();
	}

	/**
	 * Checks if the screen is touched in a certain area
	 * 
	 * @param event
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @return true or false
	 */
	protected boolean inBounds(TouchEvent event, int x, int y, int width,
			int height) {
		if (event.x > x && event.x < x + width - 1 && event.y > y
				&& event.y < y + height - 1)
			return true;
		else
			return false;
	}

	/**
	 * We iterate over each character of the string. If the current character is
	 * a space, we just advance the x-coordinate by 20 pixels. Otherwise, we
	 * calculate the x-coordinate and width of the current character's region in
	 * the numbers.png images. The character is either a digit or a dot. We then
	 * render the current character and advance the rendering x coordinate by
	 * the width of the character we've just drawn.
	 * 
	 * @param g
	 * @param line
	 * @param pix
	 * @param x
	 * @param y
	 */
	protected void drawText(Graphics g, String line, Pixmap pix, int x, int y) {
		int len = line.length();
		for (int i = 0; i < len; i++) {
			char character = line.charAt(i);

			if (character == ' ') {
				x += 20;
				continue;
			}

			int srcX = 0;
			int srcWidth = 0;
			if (character == '.') {
				srcX = 200;
				srcWidth = 10;
			} else {
				srcX = (character - '0') * 20;
				srcWidth = 20;
			}

			g.drawPixmap(pix, x, y, srcX, 0, srcWidth, 32);
			x += srcWidth;
		}
	}

	/**
	 * Calculate bonus points, depending on the time spent and the difficulty of
	 * the task.
	 * 
	 * @param extraPoints
	 */
	protected void getBonus(int extraPoints) {
		timeEnd = System.currentTimeMillis();
		totalTime = timeEnd - timeIni;
		if (totalTime <= 10000)
			oldBonus += 50 + extraPoints;
		else if ((totalTime > 10000) && (totalTime <= 20000))
			oldBonus += 25 + extraPoints;
		else
			oldBonus += 10 + extraPoints;
		bonus = "" + oldBonus;
		if (Settings.isSoundEnabled())
			Assets.getBonus().play(1);
	}

	/**
	 * 1) Adds the right answers to an ArrayList of Integer. These are the
	 * coordinates where to draw the ok.png images 2) Increases the score
	 * 
	 * @param x
	 * @param y
	 */
	protected void addRightAnswers(int x, int y) {
		Integer[] okCoordinates = new Integer[2];
		okCoordinates[0] = x;
		okCoordinates[1] = y;
		this.rightAnswers.add(okCoordinates);

		oldScore += 10;
		score = "" + oldScore;
		if (Settings.isSoundEnabled())
			Assets.getCorrect().play(1);
	}

	/**
	 * 1) Adds the wrong answers in an ArrayList of Integer. These are the
	 * coordinates where to draw the notOk.png images 2) Decrements the score
	 * (but there can be negative scores)
	 * 
	 * @param x
	 * @param y
	 */
	protected void addWrongAnswers(int x, int y) {
		Integer[] badCoordinates = new Integer[2];
		this.wrongAnswers.add(badCoordinates);
		badCoordinates[0] = x;
		badCoordinates[1] = y;
		if (oldScore >= 5) {
			oldScore -= 5;
			score = "" + oldScore;
		}
		if (Settings.isSoundEnabled())
			Assets.getWrong().play(1);
	}

	/**
	 * Draws the ok.png images on the screen
	 * 
	 * @param x
	 * @param y
	 */
	protected void drawRightAnswerUI(int x, int y) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.getOk(), x, y);
	}

	/**
	 * Draws the notOk.png images on the screen
	 * 
	 * @param x
	 * @param y
	 */
	protected void drawWrongAnswerUI(int x, int y) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.getNotOk(), x, y);
	}

	@Override
	public void update(float deltaTime) {
		// TODO Auto-generated method stub

	}

	@Override
	public void present(float deltaTime) {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
