var gameConfig = require("../GameConfig.js");
var playerConfig = require("../PlayerConfig.js");
cc.Class({
    extends: cc.Component,

    properties: {
        zOrder: {
            // ATTRIBUTES:
            default: 0,
        },
        playerStates: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    onLoad () {
        this.init();
        this.bJump = false;
        this.handrailEndX = 0;
        this.jumpSpeedX = 0;
        this.jumpDuration = 1;
    },
    init()
    {
        this.playerController = this.node.parent;
        //pConSt
        this.pConSt = this.playerController.getComponent('BasePlayerController');
        //gStatesSt
        this.gStatesSt = this.pConSt.gStatesSt;
        //pStatesSt
        this.pStatesSt = this.playerStates.getComponent('BasePlayerStates');
        // this.spine = this.node.getComponent(sp.Skeleton);
        this.armatureDisplay = this.node.getComponent(dragonBones.ArmatureDisplay);
        this.armatureDisplay.addEventListener(dragonBones.EventObject.COMPLETE, this.animationEventHandler, this);
        // this.testWarlock.getComponent(dragonBones.ArmatureDisplay);
        this.armature = this.armatureDisplay.armature();
        
    },
    run(animName,callback) {
        this.setAnim(animName,true,callback);
        this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
    },
    /**
     * @param hurdlingTag 0不在跨栏区域,1在跨栏区域
     */
    jump(animName,callback,hurdlingTag) {
        if(hurdlingTag == 0){
            if(callback){
                callback();
            }
            return;
        }

        //because action shcedule timer is not excellent so need to use update time to make animation for networking
        let hurdrailStartPX = this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].startPX;
        this.handrailEndX = hurdrailStartPX+gameConfig.handrailLength;
        // this.jumpSpeedX = (this.handrailEndX+this.pStatesSt.handrailExtlen  - this.playerController.x)*0.001/this.jumpDuration;
        // this.bJump = true;

        // jumpBy
        // cc.jumpBy(time,cc.v2(x,y),height,count)
        // cc.jumpBy(时间，停止跳跃时的坐标：原坐标 + (x,y)的值，跳跃高度，跳跃次数)
        let tw = cc.tween(this.playerController);
        // if(hurdlingTag ==0){
        //     tw.then(cc.jumpTo(0.5, cc.v2(this.pConSt.node.x+200, this.playerController.y), 0, 1));
        // }else{
        //     tw.then(cc.jumpTo(0.5, cc.v2(this.handrailEndX+200, this.playerController.y), 0, 1));
        // }
        if(hurdlingTag == 1){
            tw.then(cc.jumpTo(0.5, cc.v2(this.handrailEndX+200, this.playerController.y), 0, 1));
        }
        tw.call(function() {
            this.run('Run1',true);
            if(!callback) return;
            callback();
        }.bind(this));
        tw.start();


        this.setAnim(animName,false,function () {}.bind(this));

        this.playJumpEffect();
    },
    playJumpEffect(){},
    stagger(callback)
    {
        this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.slow;
        this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump;

        // this.setAnim('hurdling_2',false,function () {
        this.setAnim('Stagger',false,function () {
            this.run('Run1',true);
            if(!callback) return;
            callback();
        }.bind(this));
    },
    setAnim(animName,bLoop,callback)
    {
        // let track = this.spine.setAnimation(0, animName, bLoop);
        if(bLoop) 
        {
            bLoop = 0;
        }
        else{
            bLoop = 1;
        }
        this.armatureDisplay.playAnimation(animName, bLoop);
        this.callback = callback;
        // console.log('bLoop=',bLoop)
        // if (track) {
            // 注册动画的结束回调
            // this.spine.setCompleteListener((trackEntry, loopCount) => {
            //     let name = trackEntry.animation ? trackEntry.animation.name : '';
            //     if (name === animName && callback) {
            //         callback();
            //     }
            // });
        // }
    },
    animationEventHandler(event)
    {
        if (event.type === dragonBones.EventObject.COMPLETE) {
  
            if (event.animationState.name === "Run1") {
                // console.log('Run1 动作播放完毕！！！')        
                //TODO:
                this.callback();
            }
            else if (event.animationState.name === "Stagger") {
                // console.log('Stagger 动作播放完毕！！！')  
                this.callback();
            }
        }
    },
    update (dt) {
        if(this.bJump)
        {
            //count time to 0 second
            this.jumpDuration-=dt;
            //跑
            this.playerController.x += this.jumpSpeedX*dt/0.001;
            if(this.jumpDuration<=0)
            {
                this.bJump = false;
                this.handrailEndX = 0;
                this.jumpSpeedX = 0;
                this.jumpDuration = 1;
            }
        }
    }
});
