package controller.battlefield;

import model.CommandManager;
import model.ModelManager;
import view.View;
import view.math.Translator;

import com.google.common.eventbus.Subscribe;
import com.jme3.app.state.AppStateManager;
import com.jme3.input.InputManager;
import com.jme3.renderer.Camera;

import controller.Controller;
import controller.cameraManagement.IsometricCameraManager;
import de.lessvoid.nifty.Nifty;
import event.EventManager;
import event.InputEvent;
import geometry.geom2d.Point2D;

/**
 *
 */
public class BattlefieldController extends Controller {
	private boolean paused = false;

	public BattlefieldController(View view, Nifty nifty, InputManager inputManager, Camera cam) {
		super(view, inputManager, cam);

		inputInterpreter = new BattlefieldInputInterpreter(this);
		guiController = new BattlefieldGUIController(nifty, this);

		EventManager.register(this);

		cameraManager = new IsometricCameraManager(cam, 10);
	}

	@Override
	public void update(float elapsedTime) {
		// draw selection rectangle
		Point2D selStart = ((BattlefieldInputInterpreter)inputInterpreter).selectionStart;
		if(selStart != null){
			Point2D p = Translator.toPoint2D(inputManager.getCursorPosition());
			view.drawSelectionArea(selStart, p);
		} else {
			view.guiNode.detachAllChildren();
		}

		// update selectables
		CommandManager.updateSelectables(spatialSelector.getCenterViewCoord(view.rootNode));
		guiController.update();

		// udpdate army
		if(!paused) {
			ModelManager.battlefield.armyManager.update(elapsedTime);
		}
	}

	@Subscribe
	public void manageEvent(InputEvent ev) {
		guiController.update();

	}

	public void togglePause(){
		paused = !paused;
		view.actorManager.pause(paused);
	}

	@Override
	public void stateAttached(AppStateManager stateManager) {
		super.stateAttached(stateManager);
		inputManager.setCursorVisible(true);
		guiController.activate();
	}

}

@@ -1678,24 +1678,102 @@
 );%0D%0A%0D%0A%09%7D%0D%0A%0D%0A
+%09// TODO: See AppState.setEnabled =%3E use it, this is a better implementation%0D%0A
 %09public void

add TODO