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

    properties: {
        gameStates: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        aiPlayerController: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        nameIndex:0
    },
    onLoad () {
        this.gStatesScp = this.gameStates.getComponent('GameStates');
        this.aiPConScp = this.aiPlayerController.getComponent('AiPlayerController');

        this.bBeat = false;

        this.comeOutY = -110;
        this.hiddenY = -340;

        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);


        this.idleAnimName = [
            'idle',
            'idle1',
            'idle2',
        ];

        this.hookAnimName = [
            'joy21',
            'joy22',
            'joy23',
        ];

        this.jayAnimName = [
            'hook11',
            'hook12',
            'hook13',
        ];

        this.dizzAnimName = [
            'dizzy',
            'dizzy1',
            'dizzy2',
        ];

        this.currentActorName = 0;
    },
    spawn(appearDur){
        let self = this;

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

        this.currentActorName = library.randomInt(0,2);

        this.armatureDisplay.playAnimation(this.idleAnimName[this.currentActorName],1);

        //come out
        cc.tween(self.node)
            .to(0.1, { position: cc.v2(self.node.x,self.comeOutY)})
            .call(() =>
            {
                self.bBeat = true;
                self.aiPConScp.actorComeOut(self.nameIndex,appearDur);
                //delay to play
                self.scheduleOnce(function () {

                    if(!self.bBeat) return;
                    self.die();

                },appearDur);
            })
            .start()
    },
    comeOutFinished(self)
    {
        //come in
        cc.tween(self.node)
            .to(0.1, { position: cc.v2(self.node.x,self.hiddenY)})
            .start();
    },
    punch(bAi)
    {
        this.bBeat = false;
        if(0==this.nameIndex)
        {
            this.armatureDisplay.playAnimation(this.hookAnimName[this.currentActorName],1);
            if(bAi)
            {
                this.redArmatureDisplay.playAnimation("attack2",1);
            }
            else
            {
                this.blueArmatureDisplay.playAnimation("attack2",1);
            }

            return;
        }

        //left or right punch
        this.armatureDisplay.playAnimation(this.jayAnimName[this.currentActorName],1);
        if(bAi)
        {
            this.redArmatureDisplay.playAnimation("attack1",1);
        }
        else
        {
            this.blueArmatureDisplay.playAnimation("attack1",1);
        }
    },
    animationEventHandler(event)
    {
        if (event.type === dragonBones.EventObject.COMPLETE)
        {
            if (event.animationState.name === "hook11")
            {
                this.die();
            }
            else if (event.animationState.name === "hook12")
            {
                this.die();
            }
            else if (event.animationState.name === "hook13")
            {
                this.die();
            }
            else if (event.animationState.name === "joy21")
            {
                this.die();
            }
            else if (event.animationState.name === "joy22")
            {
                this.die();
            }
            else if (event.animationState.name === "joy23")
            {
                this.die();
            }
        }
    },
    die()
    {
        this.bBeat = false;
        library.removeObj(this.gStatesScp.appearMouseArr,this.node);
        this.gStatesScp.hiddenMouseArr.push(this.node);

        this.comeOutFinished(this);
    },
});
