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

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

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

EHDI.GAME.components.Active = function(guard) {
	this.guard = guard;
	this.val = false;
	this.time = 6000;
	this.activeVision = true;

	this.faceChangeTimer = new EHDI.GAME.Timer(null, 0);
	this.faceChangeTime = EHDI.GAME.utils.randomInt(4000, 5000);

	this.actionSprite = new EHDI.aka.Container();

	this.activeSprites = [];

	for(var i = 0; i < 3; i++) {
		this.activeSprites.push(new EHDI.aka.Sprite(EHDI.Assets.images["grd_fx_dot"]));
		this.activeSprites[i].position.y += 70;
		this.activeSprites[i].scale.set(0.85, 0.85);
		this.activeSprites[i].position.x = i*22;
		this.activeSprites[i].toRemove = false;
		this.actionSprite.addChild(this.activeSprites[i]);
	}

	this.actionSprite.visible = false;
	this.actionSprite.pivot.set(this.actionSprite.width/2, this.actionSprite.height/2);

	this.timeline = new TimelineLite();

	this.animationSet = false;
	this.switchFaceFunc = function() {
		this.guard.setAnimation(this.guard.currentFace.anim.idle, 0);
		this.guard.offHitboxes = false;
		this.guard.setVisionPos(this.guard.currentFace.name);
		this.guard.gArmature.removeEventListener(dragonBones.AnimationEvent.COMPLETE);
	};

	this.setBaseAnimFunc = function() {
		this.guard.setAnimation(this.anim, 0);
		this.guard.gArmature.removeEventListener(dragonBones.AnimationEvent.COMPLETE);
	}

	guard.actionSpriteContainer.addChild(this.actionSprite);
}

EHDI.GAME.components.Active.prototype.setAnim = function(playTransition) {
	this.transitionAnim = this.guard.currentFace.anim.wakeup;
	this.anim = this.guard.currentFace.anim.idle;

	if(playTransition) {
		this.guard.setAnimation(this.transitionAnim, 1);
		this.guard.gArmature.addEventListener(dragonBones.AnimationEvent.COMPLETE, this.setBaseAnimFunc, this);
	}
	else
		this.guard.setAnimation(this.anim, 0);

}

EHDI.GAME.components.Active.prototype.reset = function() {
	this.stop();
	this.faceChangeTimer = new EHDI.GAME.Timer(null, 0);
	this.faceChangeTime = EHDI.GAME.utils.randomInt(4000, 5000);
	this.val = false;
	this.timeline = new TimelineLite();
}

EHDI.GAME.components.Active.prototype.update = function() {
	var timeLeft = this.faceChangeTimer.getTimeLeft();
	if(timeLeft <= 2000) {
		if(!this.animationSet) {
			this.guard.setAnimation(this.guard.currentFace.anim.idle_flicker, 0);
			this.animationSet = true;
		}

		var i = Math.floor(timeLeft/(2001/this.activeSprites.length));
		if(i >= 0) {
			if(!this.activeSprites[i].toRemove)
				// TweenLite.to(this.activeSprites[i], 0.6, {alpha: 0});
				TweenMax.to(this.activeSprites[i], 0.6, {alpha: 0});
			this.activeSprites[i].toRemove = true;
		}
	}
}

EHDI.GAME.components.Active.prototype.start = function(fromPause) {
	this.guard.setSpriteFace(this.actionSprite, this.guard.currentFace.name);
	if(fromPause)
		this.faceChangeTimer.start();
	else
		this.faceChangeTimer = new EHDI.GAME.Timer(this.switchFace.bind(this), this.faceChangeTime);

	if(this.guard.stateChangeTimer.getTimeLeft() > 0)
		this.guard.stateChangeTimer.start();
	else {
		this.setVisibleActiveSprites(this.guard.currentFace.name);
		this.guard.stateChangeTimer = new EHDI.GAME.Timer(this.guard.setNextBehaviour.bind(this.guard), this.time);
	}
}

EHDI.GAME.components.Active.prototype.stop = function() {
	this.guard.stateChangeTimer.pause();
	this.faceChangeTimer.pause();
}

EHDI.GAME.components.Active.prototype.switchFace = function(face) {
	this.faceChangeTimer.pause();
	this.animationSet = false;

	var face = face || ((this.guard.facing["left"].val)? "right":"left");

	var i = (face == "left")? "right":"left";
	this.guard.facing[i].val = false;
	this.guard.facing[face].val = true;
	this.guard.setSpriteFace(this.actionSprite, face);

	this.guard.currentFace = this.guard.facing[face];
	this.guard.offHitboxes = true;
	this.guard.setAnimation(this.guard.currentFace.anim.turn, 1);
	this.guard.gArmature.addEventListener(dragonBones.AnimationEvent.COMPLETE, this.switchFaceFunc, this);

	this.setVisibleActiveSprites(this.guard.currentFace.name);

	this.faceChangeTimer = new EHDI.GAME.Timer(this.switchFace.bind(this), this.faceChangeTime);
}

EHDI.GAME.components.Active.prototype.setTime = function(time) {
	this.time = EHDI.GAME.utils.randomInt(time-1000, time+1000);
}

EHDI.GAME.components.Active.prototype.showActionSprite = function(val) {
	this.actionSprite.visible = val;
}

EHDI.GAME.components.Active.prototype.setVisibleActiveSprites = function() {
	this.timeline.pause(0, true);
	this.timeline.remove();
	for(var i = 0; i < this.activeSprites.length; i++) {
		var posX = this.activeSprites[i].position.x;
		this.timeline.to(this.activeSprites[i], 0.3, {alpha: 1}, "appeardots");
		this.activeSprites[i].toRemove = false;
	}
	this.timeline.play();

	if(this.activeSprites.reversed && this.guard.currentFace.name == "right") {
		this.activeSprites.reverse();
		this.activeSprites.reversed = false;
	} else if(!this.activeSprites.reversed && this.guard.currentFace.name == "left") {
		this.activeSprites.reverse();
		this.activeSprites.reversed = true;
	}
}