var DSP = (function(dsp){
  "use strict";
  //AlIASES
  var displays, protot, _super, _this;
  dsp.displays = dsp.displays || Object.create(null);
  displays = dsp.displays;

  //CONST
  var MAX_DIST_MULTIPLIER = -4;
  var EN_PATH = [];
  var _dirAngles = [];

  displays.Enemy = function(en){
    this.super.call(this);
    _super = this.super;
    this.distance = 0;
    this.currentAngle = 0;
    this.moveTimeline = null;

    this.armature = [];
    this.currentArmature;
    if(typeof en !== "string"){
      var ran = Math.random();
      en = (ran < 0.5) ? "bear" : "lion";
    }
    this.type = en;
    this.momentum = 0;

    switch(en){
      case "bear":
        this.armature[0] = DSP.DBoneFactory.createArmature("bear_idle");
        this.armature[1] = DSP.DBoneFactory.createArmature("bear_jump_and_lose");
        break;
      default:
        this.armature[0] = DSP.DBoneFactory.createArmature("lion_idle");
        this.armature[1] = DSP.DBoneFactory.createArmature("lion_jump_and_lose");
        break;
    }

    var _this = this;

    this.shadow = new EHDI.aka.Sprite(EHDI.Assets.images["enemy_shadow"]);
    this.shadow.anchor.set(0.5,0.5);

    this.currentArmature = this.armature[0];

    this.addChild(this.shadow);
    this.addChild(this.currentArmature.display);
    this.animateIdle(false);

    var isGlow = false;

    Object.defineProperty(this, "enableGlow", {
      set: function(val){
        if(isGlow === val) return;
        isGlow = val;
        this.animateIdle(val);
      },
      get: function(){
        return isGlow;
      }
    });

    this.disposeArmature = function(){
      DSP.DBoneFactory.destroyArmature(this.armature[0]);
      DSP.DBoneFactory.destroyArmature(this.armature[1]);
    }
  }

  /**
   * @static
   */
  displays.Enemy.setDirArray = function(arr){
    _dirAngles = arr;
  }

  /**
   * @static
   */
  displays.Enemy.setPath = function(val){
    EN_PATH = val;
  }

  protot = displays.Enemy.prototype = Object.create(EHDI.aka.Container.prototype)

  protot.constructor = displays.Enemy;

  protot.super = EHDI.aka.Container;

  protot.playVO = function(){
    if(this.type === "bear"){
      DSP.SoundMgr.playVO("bear_spawn");
    }else{
      DSP.SoundMgr.playVO("lion_spawn");
    }
  }

  protot.setStartPos = function(angle, dist){

    var rad = EHDI.NumberUtil.degreeToRadian(angle);
    dist = dist || MAX_DIST_MULTIPLIER;
    this.currentAngle = angle;
    this.currentDistance = dist * _tileDist;

    if(angle === _dirAngles[0] ||
      angle === _dirAngles[1] ||
      angle === _dirAngles[5]){
        this.scale.x = -1;
      }else this.scale.x = 1;
  }

  protot.initPath = function(angle){
    var e = Object.keys(EN_PATH);
    var index = _dirAngles.indexOf(angle);
    this.currentDistance = 0;
    this.currentAngle = angle;
    this.path = EN_PATH[e[index]];

    if(angle === _dirAngles[0] ||
      angle === _dirAngles[1] ||
      angle === _dirAngles[5]){
        this.scale.x = -1;
      }else this.scale.x = 1;

    this.x = this.path[0].x;
    this.y = this.path[0].y;
  }

  protot.move = function(spd, callback){
    this.currentDistance++;
    spd = 0.5;
    this.animateJump();
    if(this.currentDistance < this.path.length){
      var pos = this.path[this.currentDistance];
    }else pos = {x: DSP.SceneMgr.getStageWidth() * 0.5, y: DSP.SceneMgr.getStageHeight() * 0.57};

    var complete = function(){
      this.animateIdle();
      if(this.moveTimeline){
        this.moveTimeline.kill();
        this.moveTimeline = null;
      }
      if(typeof callback === "function") callback.apply(null, param);
    }.bind(this);

    var vAdjust = (this.currentAngle === _dirAngles[0] || this.currentAngle === _dirAngles[3]) ? 100 : 50;

    if(!this.moveTimeline){
      this.moveTimeline = new TimelineMax({onComplete: complete});
      this.moveTimeline.to(this, spd, {x: pos.x})
                       .to(this, spd * 0.5, {y: this.y - vAdjust}, spd*0.1)
                       .to(this, spd * 0.2, {y: pos.y}, spd*0.8)
    }else TweenMax.to(this, spd, {x: pos.x, y: pos.y, onComplete: complete});
    this.playVO();
  }

  protot.updateArmature = function(val){
    this.removeChild(this.currentArmature.display);
    this.currentArmature.animation.stop();
    if(typeof val !== "number"){
      this.currentArmature = (this.currentArmature === this.armature[0]) ? this.armature[0] : this.armature[1];
    }else{
      this.currentArmature = this.armature[val];
    }
    this.addChild(this.currentArmature.display);
  }

  protot.animateIdle = function(glow){
    this.updateArmature(0);
    var anim;
    if(glow && this.type === "bear") anim = "bear_select";
    if(glow && this.type === "lion") anim = "lion_select";
    if(!glow && this.type === "bear") anim = "bear_idle";
    if(!glow && this.type === "lion") anim = "lion_idle";
    this.currentArmature.animation.gotoAndPlay(anim);
  }

  protot.animateJump = function(){
    this.updateArmature(1);
    var jump = this.currentArmature.animation.animationList[1];
    this.currentArmature.animation.gotoAndPlay(jump);
  }

  protot.animateHit = function(){
    this.updateArmature(1);
    var hit = this.currentArmature.animation.animationList[0];
    this.currentArmature.animation.gotoAndPlay(hit, -1, -1, 1);
  }

  protot.animateDeath = function(callback){
    TweenMax.to(this, 0.2, {alpha: 0, ease: Power2.easeInOut, onComplete: callback} );
  }

  protot.destroy = function(){
    this.disposeArmature();
    if(this.moveTimeline) this.moveTimeline.kill();
    this.super.prototype.destroy.apply(this, arguments);
  }

  return dsp;
}(DSP || Object.create(null)))