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;

/**
 * @author "Beginning Android 4 Games Development"
 * @author (adapted by Sergi Colina)
 * 
 */
public class TreasureScreen extends GameScreen {

	boolean treasure0, treasure200, treasure300, treasure400, treasure500,
			treasure600, treasure700, treasure800, treasure900, treasure1000;
	GameState state = GameState.Ready;

	public TreasureScreen(Game game) {
		super(game);
	}

	@Override
	public void update(float deltaTime) {
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();
		if (state == GameState.Running)
			updateRunning(touchEvents, deltaTime);
		if (state == GameState.Ready)
			updateMusic();
	}

	public void updateMusic() {
		if (Settings.isSoundEnabled())
			if(treasure1000) {
				if (Assets.getPirateSong().isPlaying()) {
					Assets.getPirateSong().stop();
					Assets.getHesAPirate().play();
				}
			}
			else
				Assets.getPirateSong().play();

		Assets.getPirateSong().setLooping(true);
		Assets.getHesAPirate().setLooping(true);

		state = GameState.Running;
	}
	
	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) { // touch on screen
				if (Settings.isSoundEnabled())
					Assets.getClick().play(1);
				//Assets.getPirateSong().dispose();
				if ((Assets.getPirateSong().isPlaying()) || (Assets.getPirateSong().isLooping()))
					Assets.getPirateSong().stop();
				if ((Assets.getHesAPirate().isPlaying()) || (Assets.getHesAPirate().isLooping()))
					Assets.getHesAPirate().stop();
				game.setScreen(new MainMenuScreen(game));
				return;
			}
		}
		
		int bestScore = Settings.highscores[0];

		if (bestScore < 200)
			treasure0 = true;
		if ((bestScore >= 200) && (bestScore < 300))
			treasure200 = true;
		if ((bestScore >= 300) && (bestScore < 400))
			treasure300 = true;
		if ((bestScore >= 400) && (bestScore < 500))
			treasure400 = true;
		if ((bestScore >= 500) && (bestScore < 600))
			treasure500 = true;
		if ((bestScore >= 600) && (bestScore < 700))
			treasure600 = true;
		if ((bestScore >= 700) && (bestScore < 800))
			treasure700 = true;
		if ((bestScore >= 800) && (bestScore < 900))
			treasure800 = true;
		if ((bestScore >= 900) && (bestScore < 1000))
			treasure900 = true;
		if ((bestScore > 1000)) {
			treasure1000 = true;
			state = GameState.Ready;
		}
	}
	
	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		if (treasure0)
			g.drawPixmap(Assets.getTreasure0Screen(), 0, 0);
		if (treasure200)
			g.drawPixmap(Assets.getTreasure200Screen(), 0, 0);
		if (treasure300)
			g.drawPixmap(Assets.getTreasure300Screen(), 0, 0);
		if (treasure400)
			g.drawPixmap(Assets.getTreasure400Screen(), 0, 0);
		if (treasure500)
			g.drawPixmap(Assets.getTreasure500Screen(), 0, 0);
		if (treasure600)
			g.drawPixmap(Assets.getTreasure600Screen(), 0, 0);
		if (treasure700)
			g.drawPixmap(Assets.getTreasure700Screen(), 0, 0);
		if (treasure800)
			g.drawPixmap(Assets.getTreasure800Screen(), 0, 0);
		if (treasure900)
			g.drawPixmap(Assets.getTreasure900Screen(), 0, 0);
		if (treasure1000)
			g.drawPixmap(Assets.getTreasure1000Screen(), 0, 0);

	}

	@Override
	public void pause() {

	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {

	}
}
