package ei.game.gamestate;

import ei.engine.scene.Node;
import ei.engine.sound.Sound;
import ei.engine.sound.SoundManager;
import ei.engine.state.GameState;
import ei.game.hud.InGameHud;
import ei.game.input.InGameKeyboardInput;
import ei.game.input.InGameMouseInput;
import ei.game.player.AiPlayer;
import ei.game.player.HumanPlayer;
import ei.game.player.PlayerHandler;
import ei.game.scene.Map;
import ei.game.scene.units.APU;
import ei.game.scene.units.Tank;
import ei.game.scene.weapons.WeaponHandler;


public class InGameState extends GameState{
	private Node rootNode;
	private InGameHud hud;
	private static Map map;
	private Sound music;
	private static InGameMouseInput mouse;
	private static InGameKeyboardInput keyboard;
	
	protected static boolean network = false;
	
	public InGameState(String name){
		super(name);
	}	

	@Override
	public void init() {
		PlayerHandler.getInstance().clear();
		WeaponHandler.getInstance().clear();
		SoundManager.getInstnace().clear();
		
		rootNode = new Node("InGameNode");
		
		map = new Map(20,20);
		map.init("data/map/default");
		
		HumanPlayer player = new HumanPlayer();
		mouse = new InGameMouseInput(map,player);
		keyboard = new InGameKeyboardInput();
		super.getInput().addInput(mouse);
		super.getInput().addInput(keyboard);
		
		player.addUnit(new Tank(0, 0, player));
		player.addUnit(new Tank(1,0, player));
		player.addUnit(new Tank(2,0, player));
		player.addUnit(new APU(4, 0, player));
		player.addUnit(new APU(5, 0, player));		
		PlayerHandler.getInstance().addPlayer(player);
		
		AiPlayer ai = new AiPlayer();
		ai.addUnit(new Tank(29,33, ai));
		ai.addUnit(new Tank(30,33, ai));
		ai.addUnit(new APU(31, 33, ai));
		ai.addUnit(new APU(32, 33, ai));		
		PlayerHandler.getInstance().addPlayer(ai);
		
		rootNode.add(map.getMapNode());
		rootNode.add(PlayerHandler.getInstance().getNode());		
		rootNode.add(WeaponHandler.getInstance().getNode());
		
		hud = new InGameHud(player);
		mouse.setHud(hud);
		rootNode.add(hud.getNode());
		
		music = new Sound("music", "data/sounds/ei.ogg");
		music.loop();		
	}
	
	/**
	 * Renders the gamestate
	 */
	public void render() {
		rootNode.render();
	}

	/**
	 * Updates the gamestate
	 */
	public void update() {
		PlayerHandler.getInstance().update();
		WeaponHandler.getInstance().update();
		hud.update();
		rootNode.update();
	}
	
	/**
	 *Returns the map  of the game
	 *
	 * @return The map  of the game
	 */
	public static Map getMap() {
		return map;
	}
	
	public static boolean isNetwork(){
		return network;
	}
	
	public static void input(boolean b){
		mouse.setEnabled(b);
		keyboard.setEnabled(b);
	}
}
