package Ship;

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

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

import menus.GameMenu;
import shoot.HunterShoot;

public class Hunter01 extends BasicShip{

	protected int cpt=0;
	protected Animation warning1,warning2;
	protected String energyPercentString;
	protected int cptShoot = 0;
	
	public Hunter01(float x, float y, boolean playerOnControl) {
		super(x, y, playerOnControl);
		
		this.width = 48;
		this.height = 48;
		this.speed = 4f;
		this.speedFactor = 0f;
		this.acceleration = 0.05f;
		this.energy = 200;
		this.energy = maxEnergy;
		this.shootLevel = 0;
		this.energyPercentString = "100%";
		calculateEnergyPercent();
	}
	
	@Override
	protected void initRessources(){
		this.moveAnimations = new Animation[6];
		moveAnimations[0] = GameUtils.loadAnimation(Shared.SHIP_HUNTER, 3, 0, 0.12f, PlayMode.LOOP);
		moveAnimations[1] = GameUtils.loadAnimation(Shared.SHIP_HUNTER, 3, 1, 0.12f, PlayMode.LOOP);
		moveAnimations[2] = GameUtils.loadAnimation(Shared.SHIP_HUNTER, 3, 2, 0.12f, PlayMode.LOOP);
		moveAnimations[3] = GameUtils.loadAnimation(Shared.SHIP_HUNTER, 3, 3, 0.12f, PlayMode.LOOP);
		moveAnimations[4] = GameUtils.loadAnimation(Shared.SHIP_HUNTER, 3, 4, 0.12f, PlayMode.LOOP);
		moveAnimations[5] = GameUtils.loadAnimation(Shared.SHIP_HUNTER, 3, 5, 0.12f, PlayMode.LOOP);
		
		this.warning1 = GameUtils.loadAnimation(Shared.ANIM_WARNING, 12, 0, 0.08f,  PlayMode.LOOP);
		this.warning2 = GameUtils.loadAnimation(Shared.ANIM_WARNING, 12, 1, 0.08f,  PlayMode.LOOP);
	}
	
	/**
	 * ==========================================
	 * == MISE A JOUR DE L'OBJET               ==
	 * ==========================================
	 */
	@Override
	public void update(GameMenu game) {
		super.update(game);
		
		if(cptShoot>0){
			cptShoot--;
		}
		
		cpt++;
		if(cpt==(playerOnControl?25:45)){
			cpt = 0;
			if(energy>0){
				decreaseEnergy(1);
			}
			
		}
	}
	
	/**
	 * ==========================================
	 * == DESSIN DE L'OBJET                    ==
	 * ==========================================
	 */
	@Override
	public void renderShip(GameMenu game) {
		if(dead){
			Shared.GAME_BATCH.draw(moveAnimations[5].getKeyFrame(statetime), (int)(x-(width/2))+Shared.DECAL_X, (int)(y-(height/2)+Shared.DECAL_Y));
		}else{
			if(cptHealing > 0){
				Shared.GAME_BATCH.draw(moveAnimations[4].getKeyFrame(statetime), (int)(x-(width/2))+Shared.DECAL_X, (int)(y-(height/2)+Shared.DECAL_Y));
			}else{
				Shared.GAME_BATCH.draw(moveAnimations[speedAnimation].getKeyFrame(statetime), (int)(x-(width/2))+Shared.DECAL_X,(int)(y-(height/2)+Shared.DECAL_Y));
			}
			if(getEnergy()<=1){
				Shared.GAME_BATCH.draw(warning2.getKeyFrame(statetime),(int)(x-12+Shared.DECAL_X),(int)(y+4+Shared.DECAL_Y));
			}else if(getEnergyPercent()<0.3f){
				Shared.GAME_BATCH.draw(warning1.getKeyFrame(statetime),(int)(x-12+Shared.DECAL_X),(int)(y+4+Shared.DECAL_Y));
			}
			
			if(game.getShipOnControl() == 3){
				Shared.FONT_SMALL.setColor(0.3f, 0.3f, 0.3f, 1f);
				Shared.FONT_SMALL.draw(Shared.GAME_BATCH,energyPercentString,x+4,y+16);
				if(energyPercent<10){
					Shared.FONT_SMALL.setColor(1f, 0.2f, 0f, 1f);
				}else if(energyPercent<40){
					Shared.FONT_SMALL.setColor(1f, 0.7f, 0f, 1f);
				}else if(energyPercent>85){
					Shared.FONT_SMALL.setColor(0.3f, 1f, 0.2f, 1f);
				}else{
					Shared.FONT_SMALL.setColor(0.9f, 0.9f, 0.9f, 1f);
				}
				Shared.FONT_SMALL.draw(Shared.GAME_BATCH,energyPercentString,x+4,y+17);
			}
		}
		
	}
	
	protected void updateControls(GameMenu game){
		super.updateControls(game);
		if(Gdx.input.isKeyJustPressed(Keys.SPACE) || (Gdx.input.isKeyPressed(Keys.SPACE) && cptShoot<=0)){
			shoot(game);
		}
	}
	
	protected void shoot(GameMenu game){
		if(energy>8){
			if(playerOnControl || cptIaShoot<=0){
				cptIaShoot = 12;
				cptShoot = 12;
				decreaseEnergy(1);
				game.getShoots().addAll(createShoot());
				playShootSound();
			}
		}else{
			if(playerOnControl){
				Shared.SOUND_ERROR.play();
			}
			cptShoot = 12;
		}
	}
	
	protected List<HunterShoot> createShoot(){
		List<HunterShoot> shoots = new ArrayList<>();
		if(shootLevel == 0 || shootLevel == 2){
			shoots.add(new HunterShoot(x+14, y, 7, 0,false));
		}
		if(shootLevel == 1 || shootLevel == 2 || shootLevel == 3){
			shoots.add(new HunterShoot(x+6, y+14, 6.8f, 0.2f,false));
			shoots.add(new HunterShoot(x+6, y-14, 6.8f, -0.2f,false));
		}
		if(shootLevel == 3){
			shoots.add(new HunterShoot(x+14, y+3, 7, 0,false));
			shoots.add(new HunterShoot(x+14, y-3, 7, 0,false));
		}
		return shoots;
	}

	private void calculateEnergyPercent(){
		this.energyPercent = (energy*100)/maxEnergy;
		this.energyPercentString = String.valueOf(energyPercent);
		if(energyPercentString.length()<2){
			energyPercentString = "0"+energyPercentString;
		}
		energyPercentString +="%";
	}
	
	@Override
	protected void decreaseEnergy(int value){
		super.decreaseEnergy(value);
		calculateEnergyPercent();
	}

	@Override
	protected void increaseEnergy(int value){
		super.increaseEnergy(value);
		calculateEnergyPercent();
	}
	
	protected void playShootSound(){
		Shared.SOUND_LASER_2.play(playerOnControl?0.7f:0.5f);
	}
	
	protected void addMoveAnimation(GameMenu game){
		game.getAnimations().add(new GameAnimation(GameAnimation.ANIM_DESTROYER_HUNTER01_MOVE,x-16,y));
	}
}
