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

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,
        },
        lastTime:0,
        deltaTime:0
    },
    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');

        this.runDuration = 1000;
    },
    start() { },//override
    resetZindex() {
        this.node.zIndex = this.ctorSt.zOrder;
    }, 
    update(dt) {
        //计算deltatime  cocos 的dt坏了
        let ms = new Date().getMilliseconds();
      
        if(ms>this.lastTime)
        {
            this.deltaTime = ms-this.lastTime;
        }
        else{
            this.deltaTime = 1000-this.lastTime+ms;
        }
        this.lastTime = ms;
        
        if (this.pStatesSt.currentProState == this.pStatesSt.pProStates.end) return;

        //跑 run
        if (this.pStatesSt.currentState == this.pStatesSt.playerAnimState.run && this.pStatesSt.restMovePos!=0) {
         
            if(this.pStatesSt.targetMovePos-this.node.x<0)
            {
                this.node.x = this.pStatesSt.targetMovePos;
                this.pStatesSt.restMovePos=0;

                // if((this.pStatesSt.futureSpeed - playerConfig.acceleration/2)>0)
                // {
                    // this.pStatesSt.futureSpeed -= playerConfig.acceleration/2;
                // }
                
                this.pStatesSt.actionArr.shift();
                if(this.pStatesSt.actionArr.length==0)
                {
                    this.resetRunState();
                }
                else{
                    this.run();
                }
            }
            
            if(this.pStatesSt.restMovePos>0)
            {
                // console.log('x =',this.pStatesSt.restMovePos * this.deltaTime / this.runDuration)
                this.node.x += this.pStatesSt.restMovePos * this.deltaTime / this.runDuration;
                this.runDuration -= this.deltaTime;
                
                //不同速度下不同跑步動畫切換
                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');
                            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');
                            this.pStatesSt.currentSpeedGrade = playerConfig.playerSpeedGrade.medium;
                        }
                    }
                    this.pStatesSt.currentSpeed -= playerConfig.speedReducing;

                } else {
                    this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.idle;
                }

                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() {},//overwrite
    passedHandrail() {},//overwrite
    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.currentState != this.pStatesSt.playerAnimState.run) return;

        this.pStatesSt.futureSpeed += playerConfig.acceleration;

        if(this.pStatesSt.futureSpeed > playerConfig.playerSpeedGrade.fast)
        {
            this.pStatesSt.futureSpeed = playerConfig.playerSpeedGrade.fast;
        }

        if(this.pStatesSt.actionArr.length == 0)
        {
            this.pStatesSt.actionArr.push(this.pStatesSt.currentSpeed);
            this.run();
        }

        else if(this.pStatesSt.actionArr.length == 1)
        {
            this.pStatesSt.actionArr.push(this.pStatesSt.currentSpeed);
        }
    },
    run() {
        if (this.pStatesSt.currentSpeed <= playerConfig.playerSpeedGrade.fast &&
            this.pStatesSt.currentState == this.pStatesSt.playerAnimState.run &&
            this.pStatesSt.restMovePos == 0) 
        {
            // console.log('lastSpeed =',this.pStatesSt.lastSpeed)
            // console.log('futureSpeed =',this.pStatesSt.futureSpeed)
            if(this.pStatesSt.lastSpeed <this.pStatesSt.futureSpeed)
            {
                this.pStatesSt.currentSpeed = this.pStatesSt.lastSpeed + playerConfig.acceleration;
                this.pStatesSt.lastSpeed = this.pStatesSt.currentSpeed;
            }
            // console.log('currentSpeed =',this.pStatesSt.currentSpeed)
            this.runDuration = 1000-this.pStatesSt.currentSpeed;
            this.pStatesSt.targetMovePos = this.node.x+playerConfig.perRunDistance;
            this.pStatesSt.restMovePos = playerConfig.perRunDistance;
            this.ctorSt.run('Run1');  
        }
    },
    stop(){
        this.node.x = this.pStatesSt.targetMovePos;
        this.pStatesSt.restMovePos=0;
        this.resetRunState();
    },
    resetRunState(){
        this.pStatesSt.actionArr.length = 0;
        this.ctorSt.run('idle');
        this.pStatesSt.currentSpeedGrade = playerConfig.playerSpeedGrade.idle;
        this.pStatesSt.lastSpeed = playerConfig.playerSpeedGrade.idle;
        this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.idle;
        this.pStatesSt.futureSpeed = playerConfig.playerSpeedGrade.idle;
        this.runDuration = 1000;
    },
    speedUp_client(){},
    jump() {}//overwrite
});
