var gameConfig = require("../GameConfig.js");
var BasePlayerConfig = require("./BasePlayerConfig.js");
var playerConfig = new BasePlayerConfig();
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.callback = null;
    },
    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);
    },
    run(animName, callback) {
        this.setAnim(animName, true, callback);
        this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
    },
    /**
     * @param hurdlingTag 0不在跨栏区域,1在跨栏区域
     */
    jump(animName, callback, hurdlingTag) {
        let hurdrailStartPX = this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].startPX;
        // this.handrailEndX = hurdrailStartPX + gameConfig.handrailLength;
        this.handrailEndX = this.countingJumpDistance(hurdrailStartPX,gameConfig);

        this.callback = callback;
        this.pStatesSt.targetMovePos = this.handrailEndX + 200;
        this.pStatesSt.restMovePos = this.pStatesSt.targetMovePos - this.playerController.x;

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

        this.playJumpEffect();
    },
    countingJumpDistance(){},
    playJumpEffect() { },
    stagger(callback) {
        this.pConSt.stop();
        // this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.slow;
        this.pStatesSt.currentState = this.pStatesSt.playerAnimState.stagger;
     
        this.setAnim('hurdling_2', false, function () {
            // this.run('Run1');
            this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
            this.run('idle');
            if (!callback) return;
            callback();
        }.bind(this));
    },
    setAnim(animName, bLoop, callback) {
        let track = this.spine.setAnimation(0, animName, bLoop);

        if (track) {
            // 注册动画的结束回调
            this.spine.setCompleteListener((trackEntry, loopCount) => {
                let name = trackEntry.animation ? trackEntry.animation.name : '';
                if (name === animName && callback) {
                    callback();
                }
            });
        }
    },
    update(dt) {
        if ( this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump) {
         
            if (this.pStatesSt.targetMovePos - this.playerController.x < 0) {
                this.run('idle');
                this.playerController.x = this.pStatesSt.targetMovePos;
                this.pStatesSt.restMovePos=0;
                this.pStatesSt.jumpDuration = 1;
                if (!this.callback ) return;
                this.callback ();
            }

            if (this.pStatesSt.restMovePos > 0) {
                this.playerController.x += this.pStatesSt.restMovePos * dt / this.pStatesSt.jumpDuration;
                this.pStatesSt.jumpDuration -= dt;
            }
        }
    }
});
