package menus;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.jln.ld.Game;
import com.jln.ld.GameUtils;
import com.jln.ld.Shared;

import space.Space;

public class TitleMenu {

	private Space space;
	private Animation titleAnimation;
	private float statetime = 0f;
	private int mode;
	
	public TitleMenu(){
		this.space = new Space();
		titleAnimation = GameUtils.loadAnimation(Shared.TITLE_TITLE_ANIMATION,4,0,0.12f,PlayMode.LOOP);
	}
	
	public void update(Game game){
		space.update();
		statetime += Gdx.graphics.getDeltaTime(); 
		
		if(Gdx.input.isKeyJustPressed(Keys.SPACE) || Gdx.input.isKeyJustPressed(Keys.ENTER) ){
			if(mode == 0){
				Shared.SOUND_CONFIRM.play();
				game.goToGameMenu();
			}else{
				Gdx.app.exit();
			}
		}
		if(Gdx.input.isKeyJustPressed(Keys.DOWN)){
			if(mode == 0){
				mode = 1;
				Shared.SOUND_SELEC.play();
			}else{
				Shared.SOUND_ERROR.play();
			}
		}else if(Gdx.input.isKeyJustPressed(Keys.UP)){
			if(mode == 1){
				mode = 0;
				Shared.SOUND_SELEC.play();
			}else{
				Shared.SOUND_ERROR.play();
			}
		}
	}
	
	public void render(Game game){
		Shared.GAME_BATCH.begin();
		space.render();
		Shared.GAME_BATCH.end();
		
		Shared.MENU_BATCH.begin();
		Shared.MENU_BATCH.draw(Shared.TITLE_FRONT, 0+Shared.DECAL_X, 0+Shared.DECAL_Y);
		Shared.MENU_BATCH.draw(titleAnimation.getKeyFrame(statetime), 224+Shared.DECAL_X, 220+Shared.DECAL_Y);
		Shared.MENU_BATCH.draw(mode==0?Shared.TITLE_NEWGAME:Shared.TITLE_QUIT,240,30);
		
		if(game.getBestScore()>0){
			Shared.FONT.setColor(0.3f, 0.3f, 0.3f, 1f);
			Shared.FONT.draw(Shared.MENU_BATCH, "BEST SCORE : "+game.getBestScoreString(), 2, 14);
			Shared.FONT.setColor(0.9f, 0.9f, 0.9f, 1f);
			Shared.FONT.draw(Shared.MENU_BATCH, "BEST SCORE : "+game.getBestScoreString(), 2, 15);
		}
		Shared.MENU_BATCH.end();
	}
}
