var BarrierSuperClass = require("BarrierSuper");
cc.Class({
    extends: BarrierSuperClass,

    properties: {
        bHasCompared: false,
    },
    start: function () {
        this.finshPlayers = [];
        this.GmaeStates = cc.find('Canvas').getComponent('GameStates');
        this.GameMode = cc.find('Canvas').getComponent('GameMode');
        this.GmaeStates.EnterFinishLinePlayersArray = [];
        this.GameMode.bHasAddScore = false;
    },
    onLoad: function () {
        this.finshPlayers = [];
        this.GmaeStates = cc.find('Canvas').getComponent('GameStates');
        this.GameMode = cc.find('Canvas').getComponent('GameMode');
        this.GmaeStates.EnterFinishLinePlayersArray = [];
        this.GameMode.bHasAddScore = false;
    },
    onCollisionEnter: function (other) {
        if (other.node.name === 'PlayerCollisionLine') {


            // console.log("进入了减速区域?");
            if (!this.GameMode.bHasAddScore) {
                if (other.node.parent.parent.name == "Hero") {
                    var GameStates = cc.find('Canvas').getComponent('GameStates');

                    //这里是传给服务器判断的值，比如当前玩家的奔跑时间
                    // let testDate = new Date();
                    // let dateTime = testDate.getTime();//获取当前时间(从1970.1.1开始的毫秒数)
                    // cc.log(dateTime,'当前毫秒数');

                    //cc.log(GameStates.uploadValue,'==GameStates.uploadValue');
                    if(GameStates.uploadValue ==0) {
                        GameStates.uploadValue = cc.find('Hero').x;
                    }

                    let inputValue = GameStates.uploadValue;

                    var finishData = { FunctionName: 'callFinishLine'};
                    this.GameMode.SynchronizationFun(finishData);

                    cc.find('test').getComponent(cc.Label).string = inputValue;
                    let  deltaValue = this.GameMode.finishDeltavalue;//毫秒
                    if (!this.GmaeStates.bServer) {
                        var data = { FunctionName: 'addScore', PlayerName: 'Hero', value: inputValue ,deltaValue:deltaValue };
                        this.GameMode.SynchronizationFun(data);
                        // cc.log("--我是client，我发送给server请求加分--当前项目是"+cc.find('Canvas').getComponent('GameStates').strCureentLevel);
                    }
                    else {
                        this.GameMode.addScore(cc.find('Hero'), inputValue, true,deltaValue)
                    }
                    var CurrentPlayerNode = other.node.parent.parent;
                    this.GmaeStates.EnterFinishLinePlayersArray.push(CurrentPlayerNode);
                    var data = { FunctionName: 'PushPlayerToEnterFinishLinePlayersArray' };
                    this.GameMode.SynchronizationFun(data);

                    //有玩家到终点了
                    cc.find('Hero').getComponent('HeroControl').playAudioByName("Cheers");
                    this.SetNextStartLinePositionX();
                    this.scheduleOnce(function () {
                        this.ReadyToTransport();
                    }, 1);
                }
            }
        }
        else if (other.node.name == "PlayerCollisionLine_end") {
            var CurrentPlayerNode = other.node.parent.parent;
            // cc.log("上面先停止");
            var CurrentPlayerScript = CurrentPlayerNode.getComponent('HeroControl');
            CurrentPlayerScript.ResetSpeed_noType();
        }
    },
    ReadyToTransport: function () {
        var GameMode = cc.find('Canvas').getComponent('GameMode');
        var GameStates = cc.find('Canvas').getComponent('GameStates');

        GameMode.CheckPlayersDistance();

        var data = { FunctionName: 'CheckPlayersDistance', StartLinePositionX: GameStates.StartLinePositionX };
        GameMode.SynchronizationFun(data);
        // cc.log('我向对方发送请求瞬移');
        // this.scheduleOnce(function() {
        //     GameMode.OverPosition();
        // }, 1);
        // var data = { FunctionName: 'Transport' };
        // GameMode.SynchronizationFun(data);
    },
    SetNextStartLinePositionX: function () {
        var GameStates = cc.find('Canvas').getComponent('GameStates');
        var LevelScript = cc.find('Level').getComponent('LevelControl');
        var GameMode = cc.find('Canvas').getComponent('GameMode');
        var CurrentLevel = GameStates.strCureentLevel;
        if ('Sprint' == CurrentLevel) {
            GameStates.StartLinePositionX = LevelScript.LongJumpStartLinePositionX;
        } else if ('LongJump' == CurrentLevel) {
            GameStates.StartLinePositionX = LevelScript.HurdleStartLinePositionX;
        } else if ('Hurdle' == CurrentLevel) {
            GameStates.StartLinePositionX = LevelScript.JavelinStartLinePositionX;
        } else if ('Javelin' == CurrentLevel) {
            GameStates.StartLinePositionX = LevelScript.RideBikeStartLinePositionX;
        }
        // cc.log('current level is '+GameStates.strCureentLevel+' and X is '+GameStates.StartLinePositionX);
        // else if ('RideBike' == CurrentLevel){
        //     GameStates.StartLinePositionX = LevelScript.RideBikeStartLinePositionX;
        // }
        // var data = { FunctionName: 'SetStartLinePositionX', StartLinePositionX: GameStates.StartLinePositionX };
        // GameMode.SynchronizationFun(data);
    }


});
