package ei.game.hud;

import ei.engine.effects.BitmapText;
import ei.engine.math.Vector2f;
import ei.engine.scene.Node;
import ei.engine.scene.Sprite;
import ei.engine.ui.Button;
import ei.engine.ui.UiComponent;
import ei.engine.ui.UiHandler;
import ei.engine.ui.UiListener;
import ei.game.player.Player;
import ei.game.scene.units.APU;
import ei.game.scene.units.Bomber;
import ei.game.scene.units.Tank;

/**
 * This class handels the build buttons for the units
 * @author Ziver
 *
 */
public class InGameBuildHud implements UiListener{
	private UiHandler ui;
	private Player player;
	private BitmapText queueSize;
	
	public Button removeLastQueue;
	public Button apuBuildButton;
	public Button tankBuildButton;
	public Button bomberBuildButton;
	
	public InGameBuildHud(int x ,int y, Player p){
		player = p;
		ui = new UiHandler("BuildMenu");
		
		Vector2f size = new Vector2f(40,40);
		
		removeLastQueue = new Button("TankButton", size);
		queueSize = new BitmapText("QueueSize");
		queueSize.setText("0");
		removeLastQueue.setButtonSprite(queueSize);
		removeLastQueue.getNode().setLocation(new Vector2f(x,y));
		removeLastQueue.addListener(this);
		ui.addUi(removeLastQueue);
		
		apuBuildButton = new Button("TankButton", size);
		Sprite s1 = new Sprite("ApuButton","data/ui/apu_button.png");
		s1.setSize(size);
		apuBuildButton.setButtonSprite(s1);
		Sprite s2 = new Sprite("ApuButton_Selected","data/ui/apu_button_selected.png");
		s2.setSize(size);
		apuBuildButton.setOnTopButtonSprite(s2);
		apuBuildButton.getNode().setLocation(new Vector2f(x+(size.getX()*2),y));
		apuBuildButton.addListener(this);
		ui.addUi(apuBuildButton);
		
		tankBuildButton = new Button("TankButton", size);
		Sprite s3 = new Sprite("TankButton","data/ui/tank_button.png");
		s3.setSize(size);
		tankBuildButton.setButtonSprite(s3);
		Sprite s4 = new Sprite("TankButton_Selected","data/ui/tank_button_selected.png");
		s4.setSize(size);
		tankBuildButton.setOnTopButtonSprite(s4);
		tankBuildButton.getNode().setLocation(new Vector2f(x+(size.getX()*4),y));
		tankBuildButton.addListener(this);
		ui.addUi(tankBuildButton);
		
		bomberBuildButton = new Button("TankButton", size);
		Sprite s5 = new Sprite("BomberButton","data/ui/bomber_button.png");
		s5.setSize(size);
		bomberBuildButton.setButtonSprite(s5);
		Sprite s6 = new Sprite("BomberButton_Selected","data/ui/bomber_button_selected.png");
		s6.setSize(size);
		bomberBuildButton.setOnTopButtonSprite(s6);
		bomberBuildButton.getNode().setLocation(new Vector2f(x+(size.getX()*6),y));
		bomberBuildButton.addListener(this);
		ui.addUi(bomberBuildButton);
	}
	
	public void update(){
		queueSize.setText(""+player.getCC().getBuildQueueSize());
	}
	
	public UiHandler getUi(){
		return ui;
	}
	
	public Node getNode(){
		return ui.getNode();
	}
	
	public Player getPlayer(){
		return player;
	}
	
	public void ActionEvent(UiComponent source){
		// builds a apu
		if(source == apuBuildButton){
			player.getCC().buildUnit(new APU(player));
		}
		// builds a tank
		else if(source == tankBuildButton){
			player.getCC().buildUnit(new Tank(player));
		}
		// builds a bomber
		else if(source == bomberBuildButton){
			player.getCC().buildUnit(new Bomber(player));
		}
		
		// removes the last unit in the queue
		else if(source == removeLastQueue){
			player.getCC().removeLast();
		}
	}
}
