package ei.game.scene.units;

import ei.engine.math.Vector2f;
import ei.engine.math.Vector2i;
import ei.engine.scene.Sprite;
import ei.engine.sound.Sound;
import ei.engine.texture.AnimatedTexture;
import ei.game.network.entities.NetworkUnit;
import ei.game.player.Player;
import ei.game.scene.SelectBox;
import ei.game.scene.weapons.BomberWeapon;
import ei.game.scene.weapons.Explotion;
import ei.game.scene.weapons.Weapon;
import ei.game.scene.weapons.WeaponHandler;

/**
 * 
 * @author Jesper Lundin
 *
 */
public class Bomber extends NetworkUnit{
	private SelectBox selectionBox;
	private Sprite sprite;
	
	private Sound gunSound;
	private Sound[] moveSound;
	private Sound attackSound;
	private Sound selectSound;
	
	public Bomber(Player p) {
		super(200, p);
	}
	
	public Bomber(int x, int y, Player p){
		super(200, new Vector2i(x,y), p);
	}
	
	public void init(){
		getNode().add(sprite);
		setBuildTime(400);
		setPrice(800);
		autoAttack(false);
		

	}
	
	public void initGraphics(){
		AnimatedTexture tex = new AnimatedTexture("BomberTexture");
		tex.addAnimation("normal", new String[]{
				"data/units/bomber/bomber0000.png",
				"data/units/bomber/bomber0001.png",
				"data/units/bomber/bomber0002.png",
				"data/units/bomber/bomber0003.png"
		});
		tex.setDelay(5);
		sprite = new Sprite("Bomber",tex);
		sprite.setSize(new Vector2f(50,60));
		
		gunSound = new Sound("gunSound", "data/sounds/bomberweapon.wav");
		moveSound = new Sound[2];
		moveSound[0] = new Sound("moveSound", "data/sounds/BOMBERmove1.wav");
		moveSound[1] = new Sound("moveSound", "data/sounds/BOMBERmove2.wav");
		selectSound = new Sound("selectSound", "data/sounds/BOMBERselect.wav");
		attackSound = new Sound("attackSound", "data/sounds/BOMBERattack.wav");
		
		gunSound.setLocation(getNode().getLocation());
		moveSound[0].setLocation(getNode().getLocation());
		moveSound[1].setLocation(getNode().getLocation());
		selectSound.setLocation(getNode().getLocation());
		attackSound.setLocation(getNode().getLocation());
		
		selectionBox = new SelectBox(0,40,40,getMaxLife());
	}

	protected SelectBox getSelection() {	
		return selectionBox;
	}
	/**
	 * Returns the weapon connected to this type
	 * of unit.
	 */
	public Weapon getWeapon(Vector2f startPos) {
		return new BomberWeapon(startPos);
	}
	/**
	 * This unit type is now destroyed.
	 */
	public void destroyed(){
		WeaponHandler.getInstance().addWeapon(new Explotion(new Vector2f(getNode().getLocation().getX(), getNode().getLocation().getY())));
	}
	public Sound getGunSound() {
		return gunSound;
	}
	public Sound getMoveSound() {
		return moveSound[(int)(Math.random()*2)];
	}
	public Sound getSelectSound() {
		return selectSound;
	}
	public Sound getAttackSound() {
		return attackSound;
	}
	/**
	 * returns the velocity of the unit type.
	 * @return
	 */
	public float getVelocity() {
		return 2.5f;
	}
	/**
	 * Manages the sprite connected to this unit.
	 * @param s
	 */
	public void setSprite(Sprite s) {
		this.sprite = s;
	}
	/**
	 * returns the sprite connected to this type
	 * of unit.
	 * @return
	 */
	public Sprite getSprite() {
		return this.sprite;
	}

	@Override
	public int getMaintenanceCost() {
		return 50;
	}
}
