var gameConfig = require("../GameConfig.js");
var playerConfig = require("../PlayerConfig.js");
var webView = require("../../WebView");
var lib = require("../../Library.js");
var mgobe = require("../../Mgobe");

cc.Class({
    extends: require("BasePlayerController"),
    properties: {
        mainPlayer: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    init()//override
    {
        this._super();
        this.areaState = -1;
        this.area1 = gameConfig.handrailArea1;
        this.area2 = gameConfig.handrailArea2;

    },
    start()//override
    {
        this._super();
        //监听帧同步
         if (lib.openInWebview() && !webView.bAi)
        {
            mgobe.startFrameSync();
            mgobe.room.onRecvFrame = this.onRecvFrame.bind(this);
        }
    },
    onRecvFrame: function (event) {
        if (event.data.frame.items && event.data.frame.items.length > 0) {
            //console.log("帧广播" + JSON.stringify(event.data.frame.items));
            let item0 = event.data.frame.items[0];
            console.log("player1", this.gStatesSt.playerId,';player2',item0.playerId);
            if(this.gStatesSt.playerId == item0.playerId) return;
            console.log("帧广播", event.data.frame);

            if(item0.data.cmd == 'jump')
            {
                this.jump();
            }
            else if(item0.data.cmd == 'speedUp')
            {
                this.speedUp();
            }
        }
    },
    update(dt) {//override
        this._super();

        if(!webView.bAi) return;

        //super class return is not effect to sub class so we need to write if return every subclass
        if(this.pStatesSt.currentProState == this.pStatesSt.pProStates.end) return;
        //测试用给Ai随机速度
        if (this.pStatesSt.currentSpeed < playerConfig.playerSpeedGrade.fast)
        {
            this.pStatesSt.currentSpeed += lib.randomFromIntRange(0, 1.2);
            // this.pStatesSt.currentSpeed += lib.randomFromIntRange(0, 1);
        }

        if(this.pStatesSt.handrailArr.length == this.pStatesSt.passedHurdrailNum)return;

        this.hurdrailStartPX = this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].startPX;

        // Enter jump area 1 of handrail
        if (this.node.x > this.hurdrailStartPX+this.area1.end/2 &&
            this.node.x < this.hurdrailStartPX+this.area1.end)
        {
            if(this.areaState == -1)
            {
                this.areaState = 0;
                this.rate = lib.randomFromIntRange(0,100);
                if(this.rate<40)
                {
                    this.jump();
                }
            }
        }
        // Enter jump area 2 of handrail
        if (this.node.x > this.hurdrailStartPX + this.area2.end/2 &&
            this.node.x < this.hurdrailStartPX + this.area2.end)//-(2487-2374))
        {
            if(this.areaState == 0)
            {
                this.areaState = 1;
                if (this.rate >= 40 && this.rate < 50) {
                    this.jump();
                }

                this.schedule(function() {
                    this.areaState = -1;
                }, 1, 0, 0);
            }
        }
    },
    setEndSpeed(){//overwrite
        this.pStatesSt.intoTheEndSpeed = this.pStatesSt.currentSpeed / (this.gmSt.terminal.x+35-this.pStatesSt.endLineStopPoint);
    },
    passedHandrail()//overwrite
    {
        let ContainIndex = -1;
        for(let i=0;i<this.gStatesSt.drawedhandrailArr.length;i++)
        {
            if(this.gStatesSt.drawedhandrailArr[i].index == this.pStatesSt.passedHurdrailNum)
            {
                ContainIndex = i;
                break;
            }
        }
        if(ContainIndex!=-1)
        {
            let handrail = this.gStatesSt.drawedhandrailArr[ContainIndex].handrail.getChildByName('Kuolan2');
            let spine = handrail.getComponent(sp.Skeleton);
            spine.setAnimation(0, 'Idl1_3', false);
            this.ctorSt.stagger();
        }
    },
    jump() {//overwrite
        this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump;
        // this.ctorSt.jump('hurdling_1', function ()
        this.ctorSt.jump('Jump', function ()
        {
            this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
            this.pStatesSt.passedHurdrailNum++;
        }.bind(this));
    }
});
