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.Player = function(container) {
  this.mdArmature = EHDI.GAME.dragonFactory.buildArmature(EHDI.Assets.fetch("CBN-MaryJoseph_ske").armature[0].name);
  dragonBones.WorldClock.clock.add(this.mdArmature);
  this.mdArmature.animation.gotoAndPlay(this.mdArmature.animation.animationList[0], -1, -1, 0);
  this.mdArmature.animation.play();

  this.jArmature = EHDI.GAME.dragonFactory.buildArmature(EHDI.Assets.fetch("CBN-MaryJoseph_ske").armature[1].name);
  dragonBones.WorldClock.clock.add(this.jArmature);
  this.jArmature.animation.gotoAndPlay(this.jArmature.animation.animationList[0], -1, -1, 0);
  this.jArmature.animation.play();

  this.josephAnims = {
  	walking: this.jArmature.animation.animationList[0],
  	hiding: this.jArmature.animation.animationList[1],
  	caught: this.jArmature.animation.animationList[2]
  };

  this.mdAnims = {
  	walking: this.mdArmature.animation.animationList[0],
  	hiding: this.mdArmature.animation.animationList[1],
  	caught: this.mdArmature.animation.animationList[2]
  }

  var maryndonkey = this.mdArmature.getDisplay();
  var joseph = this.jArmature.getDisplay();

  this.people = new EHDI.aka.Container();
  this.getSprite().addChild(maryndonkey);
  this.getSprite().addChild(joseph);
  container.addChild(this.getSprite());

  this.hitBox = new EHDI.aka.Graphics();
	this.hitBox.beginFill(EHDI.GAME.HITBOX_STYLE.color, EHDI.GAME.HITBOX_STYLE.alpha);
	this.hitBox.drawRect(0, 0, this.getSprite().width-130, this.getSprite().height);
	this.hitBox.endFill();
	container.addChild(this.hitBox);

	this.hiding = false;
	this.movementSpeed = 15;
	this.isDead = false;

	this.brightnessFilter = new PIXI.filters.ColorMatrixFilter();
	this.brightnessFilter.brightness(0.9, false);
	this.getSprite().filters = [this.brightnessFilter];

  var toggleHide = function(val){
    this.hiding = val;
    this.playWalkSFX = !val;
  }

  this.tlHide = TweenLite.to(this, 0, {});

  var playWalkSFX = false;
  var self = this;
  this.footsteps = EHDI.GAME.soundManager.playSFX("footsteps");
  this.footsteps.stop();
  this.footsteps.loop = -1;

  Object.defineProperty(this, "playWalkSFX",{
    set: function(val){
      playWalkSFX = val;
      if(val){
        self.footsteps.play();
      }else self.footsteps.stop();
    },
    get: function(){
      return playWalkSFX;
    }
  })

	this.josephStart = function(e){
    var dur = null, val;
    if(e.movementID === this.josephAnims.walking){
      dur = 0.25;
      val = false;
      EHDI.GAME.soundManager.playSFX("stand");
		}else if(e.movementID === this.josephAnims.hiding){
      dur = 0.125;
      val = true;
      EHDI.GAME.soundManager.playSFX("crouch");
    }else if(e.movementID){
      this.footsteps.stop();
      EHDI.GAME.soundManager.playSFX("caught");
    }
    if(dur) this.tlHide = TweenLite.to(this, dur, {onCompleteScope: this,
            onComplete: toggleHide, onCompleteParams:[val]});
	}.bind(this);

	this.jArmature.addEventListener(dragonBones.AnimationEvent.START, this.josephStart);
	this.resetPosition();
}

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

	var textureImage = EHDI.Assets.images["CBN-MaryJoseph_tex"].baseTexture.source;

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

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

EHDI.GAME.components.Player.prototype.move = function(units, dt) {
	this.getSprite().position.x -= units*(dt * 0.01);
	this.hitBox.position.set(this.getSprite().x - (this.hitBox.width * 0.485), this.getSprite().y - this.hitBox.height);
}

EHDI.GAME.components.Player.prototype.resetPosition = function() {
	this.getSprite().position.x = EHDI.GAME.sceneManager.getStageWidth()+this.getSprite().width/2;
	this.getSprite().position.y = EHDI.GAME.sceneManager.getStageHeight()/2+this.getSprite().height;
	this.hitBox.position.set(this.getSprite().x - (this.hitBox.width * 0.485), this.getSprite().y - this.hitBox.height);
}

EHDI.GAME.components.Player.prototype.destroyAllArmature = function(){
	this.jArmature.removeEventListener(dragonBones.AnimationEvent.START, this.josephStart);
	dragonBones.WorldClock.clock.remove(this.jArmature);
	dragonBones.WorldClock.clock.remove(this.mdArmature);

	this.jArmature.dispose();
	this.mdArmature.dispose();
}

EHDI.GAME.components.Player.prototype.hideIn = function() {
	this.hiding = true;
  this.setAnimation(this.josephAnims.hiding, this.mdAnims.hiding, 1, 1);
}

EHDI.GAME.components.Player.prototype.hideOut = function() {
	// this.hiding = false;
  this.setAnimation(this.josephAnims.walking, this.mdAnims.walking, 0, 0);
}

EHDI.GAME.components.Player.prototype.isInScreen = function() {
	return this.hitBox.position.x + this.hitBox.width >= 0;
}

EHDI.GAME.components.Player.prototype.setDead = function() {
	this.isDead = true;
  this.setAnimation(this.josephAnims.caught, this.mdAnims.caught, 1, 1);
}

EHDI.GAME.components.Player.prototype.checkCollision = function(obj) {
	var combinedHalfWidths, combinedHalfHeights, vx, vy;
  var notOnScreen = (this.people.x + (this.people.width * -0.5) < -60 || this.people.x + (this.people.width * -0.5) > 840);
  // if((this.people.x + (this.people.width * -0.5) < -65) && !this.alert){
  //   alert("here")
  //   this.alert
  // }
	if(!obj || notOnScreen) return false;

	if(obj.anchor && obj.anchor.x == 1) {
		obj.x -= obj.width;
	}

	if(obj.anchor && obj.anchor.y == 1) {
		obj.y -= obj.height;
	}

	this.hitBox.centerX = this.hitBox.x + this.hitBox.width / 2;
	this.hitBox.centerY = this.hitBox.y + this.hitBox.height / 2;
	obj.centerX = obj.x + obj.width / 2;
	obj.centerY = obj.y + obj.height / 2;

	this.hitBox.halfWidth = this.hitBox.width / 2;
	this.hitBox.halfHeight = this.hitBox.height / 2;
	obj.halfWidth = obj.width / 2;
	obj.halfHeight = obj.height / 2;

	vx = this.hitBox.centerX - obj.centerX;
	vy = this.hitBox.centerY - obj.centerY;

	combinedHalfWidths = this.hitBox.halfWidth + obj.halfWidth;
	combinedHalfHeights = this.hitBox.halfHeight + obj.halfHeight;

	if (Math.abs(vx) < combinedHalfWidths)
		if (Math.abs(vy) < combinedHalfHeights)
			return true;

	return false;
};

EHDI.GAME.components.Player.prototype.getSprite = function() {
	return this.people;
}

EHDI.GAME.components.Player.prototype.setAnimation = function(jAnim, mdAnim, jRepeat, mdRepeat) {
	if(jAnim === this.josephAnims.walking && !!this.hiding){
    //add fade in before starting animation
		this.jArmature.animation.gotoAndPlay(jAnim, 0.25, -1, jRepeat);
		this.mdArmature.animation.gotoAndPlay(mdAnim, 0.25, -1, mdRepeat);
	}else{
		this.jArmature.animation.gotoAndPlay(jAnim, -1, -1, jRepeat);
		this.mdArmature.animation.gotoAndPlay(mdAnim, -1, -1, mdRepeat);
	}
}

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

EHDI.GAME.components.Player.prototype.setBrightness = function(val) {
	this.brightnessFilter.brightness(val, false);
}