let library = require("../Library");
let gameConfig = require("GameConfig");
cc.Class({
    extends: cc.Component,

    properties: {
        gameStates: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    onLoad () {
        this.gStatesScpt = this.gameStates.getComponent('GameStates');

        this.bDie = true;

        this.comeOutY = 5;
        this.hiddenY = -140;

        this.armatureDisplay = this.getComponent(dragonBones.ArmatureDisplay);

        this.blueArmatureDisplay = this.node.getChildByName('quantao_blue_ske').getComponent(dragonBones.ArmatureDisplay);
        this.redArmatureDisplay = this.node.getChildByName('quantao_ske').getComponent(dragonBones.ArmatureDisplay);
        //获取 Armatrue
        this.armature = this.armatureDisplay.armature();
        //添加动画监听
        this.armatureDisplay.addEventListener(dragonBones.EventObject.COMPLETE, this.animationEventHandler, this)
    },
    spawn(aMouse,appearDur,callback){
        let self = this;

        this.bDie = false;

        library.removeObj(this.gStatesScpt.hiddenMouseArr,this.node);
        this.gStatesScpt.appearMouseArr.push(this.node);

        this.armatureDisplay.playAnimation("idle2",1);
        //come out
        cc.tween(self.node)
            .to(0.1, { position: cc.v2(0,self.comeOutY)})
            .call(() =>
            {
                //delay to play
                self.scheduleOnce(function () {

                    self.comeOutFinished(aMouse,callback);

                    //come in
                    cc.tween(self.node)
                        .to(0.1, { position: cc.v2(0,self.hiddenY)})
                        .start()
                },appearDur);
            })
            .start()
    },
    comeOutFinished(aMouse,callback)
    {
        callback(aMouse);
        // cc.log('comeOutFinished')
    },
    punch(bAi)
    {
        this.die();
        this.armatureDisplay.playAnimation("attack23",1);
        if(bAi)
        {
            this.redArmatureDisplay.playAnimation("attack2",1);
        }
        else
        {
            this.blueArmatureDisplay.playAnimation("attack2",1);
        }
    },
    leftPunch(bAi)
    {
        this.die();
        this.armatureDisplay.playAnimation("attack13",1);
        if(bAi)
        {
            this.redArmatureDisplay.playAnimation("attack1",1);
        }
        else
        {
            this.blueArmatureDisplay.playAnimation("attack1",1);
        }
    },
    rightPunch(bAi)
    {
        this.die();
        this.armatureDisplay.playAnimation("attack13",1);
        if(bAi)
        {
            this.redArmatureDisplay.playAnimation("attack1",1);
        }
        else
        {
            this.blueArmatureDisplay.playAnimation("attack1",1);
        }
    },
    cry(callback)
    {
        this.armatureDisplay.playAnimation("cry2",1);
    },
    animationEventHandler(event)
    {

        if (event.type === dragonBones.EventObject.COMPLETE)
        {
            if (event.animationState.name === "idle2")
            {
                // console.log("idle2 动作播放完毕！！！");
                //TODO:
            }
            else if (event.animationState.name === "attack13")
            {
                // console.log("attack13 直拳动作播放完毕！！！");
            }
            else if (event.animationState.name === "attack23")
            {
                // console.log("attack23 左勾拳/右勾拳动作播放完毕！！！");
            }
            else if (event.animationState.name === "attack2")
            {
                // console.log("cry2 哭动作播放完毕！！！");
            }
        }
    },
    die()
    {
        this.bDie = true;
        library.removeObj(this.gStatesScpt.appearMouseArr,this.node);
        this.gStatesScpt.hiddenMouseArr.push(this.node);
    }
});
