var EHDI = EHDI || Object.create(null);

EHDI.GAME = EHDI.GAME || Object.create(null);

EHDI.GAME.Managers = EHDI.GAME.Managers || Object.create(null);

EHDI.GAME.Managers.GuardManager = (function() {
	var activeGuards = [];
	var guardStates = ["active", "sleeping"];
	var stateConfigurations;
	var instance;
	var guardUpdateIndex = 0;
	var objectUpdateIndex = 0;
	var objects;
	var collision = false;

	function create() {
		var public = {};

		public.spawn = function(guard) {
			if(guard.length) {
				activeGuards = [];
				activeGuards = activeGuards.concat(guard);
			}
			else
				activeGuards.push(guard);
		}

		public.removeGuard = function() {
			return activeGuards.pop();
		}

		public.activeGuardCount = function() {
			return activeGuards.length;
		}

		public.update = function(player, dt) {
			if(player.isDead)
				return;

			objects = EHDI.GAME.ObjectManager.getActiveObjects();

			for(var i = 0, len = public.activeGuardCount(); i < len; i++) {
				activeGuards[i].update();
				if(!player.hiding && (player.checkCollision(activeGuards[i].getActiveVision()))) {
					activeGuards[i].setCaught();
					player.setDead();
					activeGuards[i].pauseActions();
				}
				if(player.checkCollision(activeGuards[i].getActiveVision(true))) {
					player.toLight = true;
				}
				for(var j = 0; j < objects.length; j++) {
					if(objects[j].checkCollision(activeGuards[i].getActiveVision(true))) {
						objects[j].toLight = true;
					}
				}
			}

			if(player.toLight) {
				player.setBrightness(1.3);
				player.toLight = false;
			} else
				player.setBrightness(0.9);

			for (var i = objects.length - 1; i >= 0; i--) {
				if(objects[i].toLight) {
					objects[i].setLighted(true);
					objects[i].toLight = false;
				} else {
					objects[i].setLighted(false);
				}
			}
		}

		public.startGuards = function(fromPause) {
			for(var i = 0, len = public.activeGuardCount(); i < len; i++) {
				activeGuards[i].startActions(fromPause);
			}
		}

		public.pauseGuards = function() {
			for(var i = 0, len = public.activeGuardCount(); i < len; i++) {
				activeGuards[i].pauseActions();
			}
		}

		public.checkCollisions = function(obj, excludedGuard) {
			for(var i = 0, len = public.activeGuardCount(); i < len; i++) {
				if(activeGuards[i] != excludedGuard)
					if(activeGuards[i].checkCollision(obj))
						return true;
			}
			return false;
		}

		public.removeActiveGuards = function(container, dispose) {
			collision = false;
			guardUpdateIndex = 0;
			objectUpdateIndex = 0;
			for(var i = 0, len = public.activeGuardCount(); i < len; i++) {
				if(dispose)
					activeGuards[i].destroyAllArmature();
				else
					activeGuards[i].recycle();
				container.removeChild(activeGuards[i]);
			}

			if(dispose)
				activeGuards = [];
		}

		public.getStateConfigs = function() {
			stateConfigurations = stateConfigurations || EHDI.GAME.utils.permute(guardStates);
			return stateConfigurations;
		}

		return public;
	}

	return {
		getInstance: function() {
			if(!instance) {
				instance = create();
			}
			return instance;
		}
	};
})();