var gameConfig = require("../GameConfig.js");
var playerConfig = require("../PlayerConfig.js");

cc.Class({
    extends: cc.Component,
    properties: {
        gameMode: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        charactor: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        playerStates: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    onLoad() {
        this.init();
    },
    init() {
        //gameModeScript
        this.gmSt = this.gameMode.getComponent('GameMode');
        //gStatesSt
        this.gStatesSt = this.gmSt.gStatesSt;
        //ctorSt
        this.ctorSt = this.charactor.getComponent('BaseCharactor');
        //pStatesSt
        this.pStatesSt = this.playerStates.getComponent('BasePlayerStates');
    },
    start() { },//override
    resetZindex() {
        this.node.zIndex = this.ctorSt.zOrder;
    },
    run() {
        // this.ctorSt.run('Run1');
        this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
        this.ctorSt.setAnim('idle', false);
        // this.pStatesSt.currentState = this.pStatesSt.playerAnimState.idle;
    },
    update(dt) {
        if (this.pStatesSt.currentProState == this.pStatesSt.pProStates.end) return;
        // console.log(this.pStatesSt.currentSpeed);

        if (this.pStatesSt.currentState == this.pStatesSt.playerAnimState.run) {
            //跑
            this.node.x += this.pStatesSt.currentSpeed;

            //如果不加速跑,自动逐渐减速
            // if (this.pStatesSt.currentSpeed > playerConfig.playerSpeedGrade.idle) {
            //     if (this.pStatesSt.currentSpeed - playerConfig.speedReducing > playerConfig.playerSpeedGrade.idle) {
            //         this.pStatesSt.currentSpeed -= playerConfig.speedReducing;
            //     } else {
            //         this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.idle;
            //     }
            // }
            if (this.pStatesSt.currentSpeed <= playerConfig.playerSpeedGrade.idle) {
                this.ctorSt.setAnim('idle',false);
                this.pStatesSt.currentSpeedGrade = playerConfig.playerSpeedGrade.idle;
            } else {
                if (this.pStatesSt.currentSpeed - playerConfig.speedReducing > playerConfig.playerSpeedGrade.idle) {

                    if (this.pStatesSt.currentSpeed < playerConfig.playerSpeedGrade.medium) {
                        if (this.pStatesSt.currentSpeedGrade != playerConfig.playerSpeedGrade.slow) {
                            this.ctorSt.run('Run1',true);
                            this.pStatesSt.currentSpeedGrade = playerConfig.playerSpeedGrade.slow;
                        }
                    }
                    else if (this.pStatesSt.currentSpeed > playerConfig.playerSpeedGrade.medium &&
                        this.pStatesSt.currentSpeed < playerConfig.playerSpeedGrade.fast) {
                        if (this.pStatesSt.currentSpeedGrade != playerConfig.playerSpeedGrade.medium) {
                            this.ctorSt.run('Run2',true);
                            this.pStatesSt.currentSpeedGrade = playerConfig.playerSpeedGrade.medium;
                        }
                    }
                    this.pStatesSt.currentSpeed -= playerConfig.speedReducing;
                } else {
                    this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.idle;
                }
            }

            // passed front of Handrail
            if (this.pStatesSt.passedHurdrailNum <= this.pStatesSt.handrailArr.length - 1) {
                if (this.node.x >= this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].startPX + gameConfig.handrailLength + 20) {
                    this.passedHandrail();
                    this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].bFallDown = 1;
                    this.pStatesSt.passedHurdrailNum++;
                }
            }
        }

        //Enter end area
        if (this.node.x > this.gmSt.terminal.x - 320 &&
            this.pStatesSt.currentProState < this.pStatesSt.pProStates.intoEnd) {
            this.pStatesSt.currentProState = this.pStatesSt.pProStates.intoEnd;
            this.setEndSpeed();
        }
        //Prepare to endLine
        if (this.pStatesSt.currentProState == this.pStatesSt.pProStates.intoEnd) {
            if (this.pStatesSt.currentSpeed <= 0) return;
            this.pStatesSt.currentSpeed -= this.pStatesSt.intoTheEndSpeed;
        }
        //Enter endline
        if (this.node.x > this.gmSt.terminal.x - this.pStatesSt.endLineStopPoint && this.gmSt.terminal.active) {
            this.enterEndline();
        }
    },
    setEndSpeed() {
    },
    passedHandrail() {
    },
    enterEndline() {
        this.pStatesSt.currentSpeed = 0;
        this.ctorSt.setAnim('idle', false);
        this.pStatesSt.currentState = this.pStatesSt.playerAnimState.default;
        this.pStatesSt.currentProState = this.pStatesSt.pProStates.end;
        this.gStatesSt.arrEndPArr.push(this.pStatesSt);
    },
    speedUp() {

        this.pStatesSt.runTimes++;

        if (this.pStatesSt.currentSpeed <= playerConfig.playerSpeedGrade.fast &&
            this.pStatesSt.currentState == this.pStatesSt.playerAnimState.run) {
            this.pStatesSt.currentSpeed += playerConfig.touchToAddSpeed;
        }
    },
    jump() {
    },
});
