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.Guard = function(container) {
    this.gArmature = EHDI.GAME.dragonFactory.buildArmature(EHDI.Assets.fetch("guard_an_ske").armature[0].name);
    dragonBones.WorldClock.clock.add(this.gArmature);

    this.guardSprite = this.gArmature.getDisplay();
    this.guardSprite.position.y += 330;

    this.guard = new EHDI.aka.Container();
    this.getSprite().addChild(this.guardSprite);
    container.addChild(this.getSprite());

		this.getSprite().pivot.x = this.getSprite().width * 0.4;
		this.getSprite().pivot.y = this.getSprite().height * 0.5;

	this.currentFace;
	this.facing = {
		left: {
			val:false,
			name: "left",
			anim: {
				idle: this.gArmature.animation.animationList[14],
				idle_flicker: this.gArmature.animation.animationList[8],
				idle_to_caught: this.gArmature.animation.animationList[13],
				idle_to_sleeping: this.gArmature.animation.animationList[12],
				turn: this.gArmature.animation.animationList[3],
				sleeping: this.gArmature.animation.animationList[11],
				caught: this.gArmature.animation.animationList[15],
				wakeup: this.gArmature.animation.animationList[9]
			}},
		right: {
			val:false,
			name: "right",
			anim: {
				idle: this.gArmature.animation.animationList[0],
				idle_flicker: this.gArmature.animation.animationList[7],
				idle_to_caught: this.gArmature.animation.animationList[4],
				idle_to_sleeping: this.gArmature.animation.animationList[5],
				turn: this.gArmature.animation.animationList[10],
				sleeping: this.gArmature.animation.animationList[2],
				caught: this.gArmature.animation.animationList[4],
				wakeup: this.gArmature.animation.animationList[6]
			}}
	};

  this.guardStart = function(e){
    switch(e.movementID){
      case this.facing.left.anim.turn:
      case this.facing.right.anim.turn:
        EHDI.GAME.soundManager.playSFX("switch_look");
        break;
      case  this.facing.left.anim.wakeup:
      case  this.facing.right.anim.wakeup:
        EHDI.GAME.soundManager.playSFX("soldier_wake_up");
        break;
      default:
    }
  }.bind(this);

	this.gArmature.addEventListener(dragonBones.AnimationEvent.START, this.guardStart);

	this.behaviour = [];
	this.behaviourIndex = 0;
	this.goingBack = false;

	// this.caughtSprite = EHDI.Assets.fetch("guard_caught").texture;
	this.caughtSprite = EHDI.Assets.images["guard_caught"];
	this.actionSpriteContainer = new EHDI.aka.Container();
	this.actionSpriteContainer.position.set(0, -this.height/2 - 80);

	this.caughtAction = new EHDI.aka.Container();
	var fx_exclam1 = new EHDI.aka.Sprite(EHDI.Assets.images["fx_exclam1"]);
	var fx_exclam2 = new EHDI.aka.Sprite(EHDI.Assets.images["fx_exclam2"]);
	fx_exclam2.position.x += 26;
	fx_exclam2.position.y += 75;
	this.caughtAction.addChild(fx_exclam1);
	this.caughtAction.addChild(fx_exclam2);
	this.caughtAction.visible = false;
	this.actionSpriteContainer.addChild(this.caughtAction);

	this.states = {
		active: new EHDI.GAME.components.Active(this),
		sleeping: new EHDI.GAME.components.Sleeping(this)
	};
	this.stateChangeTimer = new EHDI.GAME.Timer(null, 0);
	this.currentState;

	this.getSprite().addChild(this.actionSpriteContainer);

	//lightReach is the hitbox that the objects react to. makes object lighting more realistic
	this.lightReach = new EHDI.aka.Graphics();
	this.lightReach.beginFill(0xFF00FF, EHDI.GAME.HITBOX_STYLE.alpha);
	this.lightReach.drawRect(0, 0, 300, 150);
	this.lightReach.endFill();
	container.addChild(this.lightReach);

	this.hitBox = new EHDI.aka.Graphics();
	this.hitBox.beginFill(0xFF0000, EHDI.GAME.HITBOX_STYLE.alpha);
	this.hitBox.drawRect(0, 0, 240, 150);
	this.hitBox.endFill();
	container.addChild(this.hitBox);

	this.offHitboxes = false;

	this.caughtTimer = new EHDI.GAME.Timer();
	this.caughtFunc = function() {
		this.gArmature.removeEventListener(dragonBones.AnimationEvent.COMPLETE, arguments.callee);
		this.setAnimation(this.currentFace.anim.caught, 0);
	}
}

EHDI.GAME.components.Guard.initDB= function(){
	if(!!EHDI.GAME.components.Guard.doneDB) return;
	EHDI.GAME.components.Guard.doneDB = true;

	var textureImage = EHDI.Assets.images["guard_an_tex"].baseTexture.source;

	var textureData = EHDI.Assets.fetch("guard_an_tex");
	EHDI.GAME.dragonFactory.addTextureAtlas(new dragonBones.TextureAtlas(textureImage, textureData));

	var skeleton = EHDI.Assets.fetch("guard_an_ske");
	EHDI.GAME.dragonFactory.addDragonBonesData(dragonBones.DataParser.parseDragonBonesData(skeleton));
}

EHDI.GAME.components.Guard.prototype.update = function(dt) {
	this.currentState.update();
}

EHDI.GAME.components.Guard.prototype.initBehaviour = function(initial, behaviour) {
	this.behaviour = behaviour;
	this.facing[initial.face].val = true;
	this.currentFace = this.facing[initial.face];
	this.setVisionPos(initial.face);

	this.switchState(initial.state);
}

//set hitbox position
EHDI.GAME.components.Guard.prototype.setVisionPos = function(face) {
	if(face === "left") {
		this.hitBox.position.set(this.getSprite().x-495, this.getSprite().y);
		this.lightReach.position.set(this.getSprite().x-515, this.getSprite().y);
	}
	else {
		this.hitBox.position.set(this.getSprite().x-120, this.getSprite().y);
		this.lightReach.position.set(this.getSprite().x-160, this.getSprite().y);
	}
}

EHDI.GAME.components.Guard.prototype.getNextBehaviour = function() {
	if(this.behaviourIndex >= this.behaviour.length-1) {
		this.goingBack = true;
		this.behaviourIndex--;
	} else if(this.behaviourIndex == 0) {
		this.goingBack = false;
		this.behaviourIndex++;
	} else if(this.goingBack) {
		this.behaviourIndex--;
	} else {
		this.behaviourIndex++;
	}
}

EHDI.GAME.components.Guard.prototype.setNextBehaviour = function() {
	this.currentState.stop();
	this.getNextBehaviour();
	this.switchState(this.behaviour[this.behaviourIndex]);
}

EHDI.GAME.components.Guard.prototype.switchState = function(state) {
	this.stateChangeTimer.pause();
	var i = (state == "active")? "sleeping":"active";
	this.states[i].val = false;
	this.states[i].showActionSprite(false);

	this.states[state].val = true;

	if(this.currentState)
		this.states[state].setAnim(true);
	else
		this.states[state].setAnim(false);

	this.currentState = this.states[state];

	this.currentState.showActionSprite(true);

	this.currentState.start();
}

EHDI.GAME.components.Guard.prototype.getActiveVision = function(offsetted) {
	if(!this.currentState.activeVision || this.offHitboxes)
		return null;

	if(offsetted)
		return this.lightReach;
	else
		return this.hitBox;
}

EHDI.GAME.components.Guard.prototype.startActions = function(fromPause) {
	this.currentState.start(fromPause);
	this.caughtTimer.start();
}

EHDI.GAME.components.Guard.prototype.pauseActions = function() {
	this.currentState.stop();
	this.caughtTimer.pause();
}

EHDI.GAME.components.Guard.prototype.setSpriteFace = function(sprite, face) {
	if(face == "left") {
		sprite.scale.x = -1;
	} else {
		sprite.scale.x = 1;
	}
}

EHDI.GAME.components.Guard.prototype.setCaught = function() {
	var justCaught = function() {
		this.caughtAction.visible = false;
		this.setAnimation(this.currentFace.anim.idle_to_caught, 1)
		this.gArmature.addEventListener(dragonBones.AnimationEvent.COMPLETE, this.caughtFunc, this);
	}
	this.stateChangeTimer.pause();
	this.currentState.actionSprite.visible = false;
	this.setSpriteFace(this.caughtAction, this.currentFace.name);
	this.caughtAction.visible = true;

	// TweenLite.from(this.caughtAction, 0.2, {y: this.caughtAction.y + 50, alpha:0});
	TweenMax.from(this.caughtAction, 0.2, {y: this.caughtAction.y + 50, alpha:0});
	this.caughtTimer = new EHDI.GAME.Timer(justCaught.bind(this), 1000);
	// setTimeout(justCaught.bind(this), 1000);
}

EHDI.GAME.components.Guard.prototype.recycle = function() {
	this.pauseActions();

	this.facing["left"].val = false;
	this.facing["right"].val = false;
	this.states["active"].reset();
	this.states["sleeping"].reset();
	this.stateChangeTimer.pause();
	this.stateChangeTimer = new EHDI.GAME.Timer(null, 0);
	this.currentState = null;

	this.behaviour = [];
	this.behaviourIndex = 0;
	this.goingBack = false;
}

EHDI.GAME.components.Guard.prototype.setAnimation = function(anim, repeat) {
	this.gArmature.animation.gotoAndPlay(anim, -1, -1, repeat);
}

EHDI.GAME.components.Guard.prototype.getSprite = function() {
	return this.guard;
}

EHDI.GAME.components.Guard.prototype.destroyAllArmature = function() {
	this.gArmature.removeEventListener(dragonBones.AnimationEvent.START, this.guardStart);
	dragonBones.WorldClock.clock.remove(this.gArmature);
	this.gArmature.dispose();
}

EHDI.GAME.components.Guard.prototype.destroy = function(){
	if(this.timeline) this.timeline.kill();
  EHDI.aka.Container.prototype.destroy.call(this, {children:true});
}