package de.samdev.colorrunner.game.world;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.math.Rectangle;

import java.util.ArrayList;
import java.util.List;

import de.samdev.colorrunner.CRGame;
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.PlayerEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.FloorTileEntity;
import de.samdev.colorrunner.game.world.map.provider.MapProvider;
import de.samdev.colorrunner.input.GameInputListener;
import de.samdev.colorrunner.screens.gameScreen.GameScreen;

public class CRGameWorld implements GameInputListener {
	private Preferences prefs = Gdx.app.getPreferences("settings");
	private Preferences endlessGameInfos = Gdx.app.getPreferences("endlessGameInfos");
	public PlayerEntity player;
	public List<CRGameEntity> entities;

	public FPSCounter fps = new FPSCounter();
	public Rectangle camViewBoundaries = new Rectangle();

	public MapProvider mapprovider;
	public Music music;
	public int scoreMeter = 0;

	public CRGameWorld(MapProvider prov) {
		mapprovider = prov;

		music = CRGame.manager.get("sound/mainsound.mp3", Music.class);
		music.setLooping(true);

		if(prefs.getBoolean("sound", true))
			music.play();
		else
			music.stop();

		reinitialize();
	}

	private void reinitialize() {
		entities = new ArrayList<CRGameEntity>();

		addEntity(player = new PlayerEntity(this, 40, 290));

		mapprovider.init(this);
	}

	public void update(float delta) {
		fps.Inc();

		Gdx.input.setCatchBackKey(true);
		if(Gdx.input.isKeyPressed(Input.Keys.BACK) || Gdx.input.isKeyPressed(Input.Keys.BACKSPACE)) {
			Gdx.input.setCatchBackKey(false);
			//TODO ::
			//if(mapprovider.isNextEndlessMenu())
			//	((Game) Gdx.app.getApplicationListener()).setScreen(new EndlessGameMenu(music));
			//else
			//	((Game) Gdx.app.getApplicationListener()).setScreen(new MainMenu());
		}

		for (int i = entities.size()-1; i >= 0; i--) {
			CRGameEntity ent = entities.get(i);
			
			if (ent.active)
				ent.update(delta);
			else
				entities.remove(i);
		}

		float posi = player.getPosition().x;
		if((posi / 75) >= (float)scoreMeter)
		{
			scoreMeter++;
		}
		if(player.getPosition().y < - FloorTileEntity.FLOORTILE_HEIGHT|| player.getPosition().y > 16 * FloorTileEntity.FLOORTILE_HEIGHT )
		{
			checkHighscore();
			setTries();
			setLastBest();
			CRGame.Inst().setScreen(new GameScreen(this.mapprovider.createNew()));
			music.stop();
		}

		mapprovider.update(this, player.bounds);
	}

	public CRGameEntity addEntity(CRGameEntity ent) {
		entities.add(ent);

		return ent;
	}
	
	@Override
	public void doJump() {
		player.jumpPressed();
	}

	@Override
	public void switchColor(SwipeDirection sd) {
		player.switchPhase(sd);
	}

	@Override
	public void reset() {
		reinitialize();
	}

	private void checkHighscore(){
		int highscore = endlessGameInfos.getInteger("Highscore", 0);
		if(highscore < scoreMeter){
			endlessGameInfos.putInteger("Highscore", scoreMeter);
		}
		endlessGameInfos.flush();
	}
	private void setTries(){
		int tries = endlessGameInfos.getInteger("Tries", 0);
		tries++;
		endlessGameInfos.putInteger("Tries", tries);
		endlessGameInfos.flush();
	}
	private void setLastBest(){
		int lastBest = endlessGameInfos.getInteger("lastBest", 0);
		if(lastBest < scoreMeter){
			endlessGameInfos.putInteger("lastBest", scoreMeter);
		}
		endlessGameInfos.flush();
	}


}
