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.GameUtils;
import com.jln.ld.Shared;

public class GameHud {
	
	private Animation lowPowerAnimation, outOfPowerAnimation, destroyAnimation, pressSpaceAnimation;
	private float statetime = 0f;
	private boolean onPause = false;

	public GameHud(){
		lowPowerAnimation = GameUtils.loadAnimation(Shared.ANIM_INFORMATION, 4, 0, 0.12f, PlayMode.LOOP);
		outOfPowerAnimation = GameUtils.loadAnimation(Shared.ANIM_INFORMATION, 4, 1, 0.12f, PlayMode.LOOP);
		destroyAnimation = GameUtils.loadAnimation(Shared.ANIM_INFORMATION, 4, 2, 0.12f, PlayMode.LOOP);
		pressSpaceAnimation = GameUtils.loadAnimation(Shared.ANIM_INFORMATION, 4, 3, 0.12f, PlayMode.LOOP);
	}
	
	public void initGameHud(){
		onPause = false;
	}
	
	/**
	 * ==========================================
	 * == MISE A JOUR DE L'OBJET               ==
	 * ==========================================
	 */
	public void update(GameMenu game){
		if(!game.isGameOver()){
			if(Gdx.input.isKeyJustPressed(Keys.ESCAPE)){
				onPause = !onPause;
				Shared.SOUND_CONFIRM.play();
			}
		}
		
		if(onPause || game.isGameOver()){
			if(Gdx.input.isKeyJustPressed(Keys.SPACE) || Gdx.input.isKeyJustPressed(Keys.ENTER) ){
				Shared.SOUND_CONFIRM.play();
				if(onPause){
					onPause = !onPause;
					
				}if(game.isGameOver()){
					game.goToTitle();
				}
				
			}
		}
		
	}
	
	/**
	 * ==========================================
	 * == DESSIN DE L'OBJET                    ==
	 * ==========================================
	 */
	public void render(GameMenu game){
		int shipControl = game.getShipOnControl();
		Shared.MENU_BATCH.draw(Shared.HUD_HUNTER_BACK[game.getHunter01().isDead()?2:shipControl==1?1:0][0],2,0);
		Shared.MENU_BATCH.draw(Shared.HUD_HUNTER_BACK[game.getHunter02().isDead()?2:shipControl==2?1:0][0],204,0);
		Shared.MENU_BATCH.draw(Shared.HUD_SUPPLY_BACK[game.getSupplyShip().isDead()?2:shipControl==3?1:0][0],406,0);
		
		Shared.MENU_BATCH.draw(game.getShipOnControl()==1?Shared.HUD_HUNTER01B:Shared.HUD_HUNTER01, 2, 1);
		Shared.MENU_BATCH.draw(game.getShipOnControl()==2?Shared.HUD_HUNTER02B:Shared.HUD_HUNTER02, 204, 1);
		
		Shared.MENU_BATCH.draw(game.getHunter01().getEnergy()<10?Shared.HUD_HUNTER_BAR3:game.getHunter01().getEnergyPercent()<0.4f?Shared.HUD_HUNTER_BAR2:Shared.HUD_HUNTER_BAR1, 66, 4, 0, 0, (int)(132f*game.getHunter01().getEnergyPercent()), 8);
		Shared.MENU_BATCH.draw(game.getHunter02().getEnergy()<10?Shared.HUD_HUNTER_BAR3:game.getHunter02().getEnergyPercent()<0.4f?Shared.HUD_HUNTER_BAR2:Shared.HUD_HUNTER_BAR1, 268, 4, 0, 0, (int)(132f*game.getHunter02().getEnergyPercent()), 8);
		
		for(int i=0;i<game.getSupplyShip().getEnergy();i++){
			Shared.MENU_BATCH.draw(Shared.HUD_SUPPLY_BAR[game.getSupplyShip().getEnergy()==1?2:game.getSupplyShip().getEnergy()==2?1:0][0],(516+(i*24)),4);
		}
		
		if(!game.isGameOver()){
			if(game.getPlayerShip().isDead()){
				Shared.MENU_BATCH.draw(destroyAnimation.getKeyFrame(statetime), (Shared.GAME_WIDTH/2)-44, 330);
			}else if(game.getShipOnControl() != 3 && game.getPlayerShip().getEnergy() < 10){
				Shared.MENU_BATCH.draw(outOfPowerAnimation.getKeyFrame(statetime), (Shared.GAME_WIDTH/2)-44, 330);
			}else if( game.getShipOnControl() != 3 && game.getPlayerShip().getEnergyPercent() < 0.4f){
				Shared.MENU_BATCH.draw(lowPowerAnimation.getKeyFrame(statetime), (Shared.GAME_WIDTH/2)-44, 330);
			}
		}
		
		if(game.isGameOver()){
			Shared.MENU_BATCH.draw(Shared.HUD_GAMEOVER, 160, 180);
		}else if(onPause){
			Shared.MENU_BATCH.draw(Shared.HUD_PAUSE, 160, 180);
		}
		
		if(game.isGameOver() || onPause){
			Shared.MENU_BATCH.draw(pressSpaceAnimation.getKeyFrame(statetime), (Shared.GAME_WIDTH/2)-44, 180);
		}
		
		
		Shared.FONT_SMALL.setColor(0.3f, 0.3f, 0.3f, 1f);
		Shared.FONT_SMALL.draw(Shared.MENU_BATCH,game.getPointsString(),558,355);
		Shared.FONT_SMALL.draw(Shared.MENU_BATCH,String.valueOf(game.getTimer()),6,355);
		Shared.FONT_SMALL.setColor(0.9f, 0.9f, 0.9f, 1f);
		Shared.FONT_SMALL.draw(Shared.MENU_BATCH,game.getPointsString(),558,356);
		
		statetime += Gdx.graphics.getDeltaTime(); 
		
	}
	
	public boolean isOnPause(){
		return onPause;
	}
}
