const PlayerStateStatic = require('PlayerState');
cc.Class({
    extends: cc.Component,

    properties: {

        Hero: {
            default: null,
            type: cc.Node
        },
        StayInFrontLongAccelerationBand:null,
        IsPlayingAudio:false,
        YellowLinePrefab:null,
        HeroOldPositionX:0,
        CanCreateYellowArea:false
    },
    onLoad:function () {
        //todo 开始不能点击
        var Hero = cc.find('Hero');
        var  PlayerStateScript = Hero.getComponent("PlayerState");
        PlayerStateScript.TouchControlEnableTouch = false;
        this.HeroControlScript = this.Hero.getComponent("HeroControl");

        this.YellowLinePrefab = null;

    },
    start : function () {
        cc.director.getCollisionManager().enabled = true;
        this.touchClick(true);
    },
    DrawBox : function () {
        //刚体包围盒
        cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_aabbBit |
            cc.PhysicsManager.DrawBits.e_pairBit |
            cc.PhysicsManager.DrawBits.e_centerOfMassBit |
            cc.PhysicsManager.DrawBits.e_jointBit |
            cc.PhysicsManager.DrawBits.e_shapeBit
        ;
    },

    touchClick : function (b) {
        // cc.log("按屏幕了",this.enableTouch,this.TouchControlEnableTouch);

        var TouchLayout = cc.find("TouchLayout");
        var self = this;

        if (b) {
            TouchLayout.on(cc.Node.EventType.TOUCH_START, function (event) {
                this.TOUCH_START(true);

            }.bind(this), self.node);

            self.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {

            }.bind(this), self.node);

            self.node.on(cc.Node.EventType.TOUCH_END, function (event) {
                this.TOUCH_END(true);
            }.bind(this), self.node);


        }else{
            TouchLayout.off(cc.Node.EventType.TOUCH_START, function (event) {
                this.TOUCH_START(true);

            }.bind(this), self.node);

            self.node.off(cc.Node.EventType.TOUCH_MOVE, function (event) {

            }.bind(this), self.node);

            self.node.off(cc.Node.EventType.TOUCH_END, function (event) {
                this.TOUCH_END(true);
            }.bind(this), self.node);

        }




    },

    TOUCH_START:function()
    {
        var Hero = cc.find('Hero');
        var scene = cc.director.getScene();
        var Level = scene.getChildByName("Level");
        var HeroControlScript = Hero.getComponent("HeroControl");
        var  PlayerStateScript = Hero.getComponent("PlayerState");
        // var  player = Hero.getChildByName('Player');
        // cc.log(PlayerStateScript.enableTouch+'+++++++PlayerStateScript.enableTouch');
        // cc.log(PlayerStateScript.TouchControlEnableTouch+'+++++++PlayerStateScript.TouchControlEnableTouch');


        // cc.log("按下去 判断",PlayerStateScript.enableTouch,PlayerStateScript.TouchControlEnableTouch);

        if(!PlayerStateScript.enableTouch) return;

        if(!PlayerStateScript.TouchControlEnableTouch) return;

        var HeroControlPositionX = HeroControlScript.node.position.x -360;

        //setLine
        if(PlayerStateScript.CurrentInZoneType!=PlayerStateStatic.ZoneType.FrontLongAccelerationBand
            && PlayerStateScript.CurrentInZoneType!=PlayerStateStatic.ZoneType.CenterLongAccelerationBand
            && HeroControlScript.PlayerStateScript.BStagger == false)
        {
            var PlayerCollisionLinePosition = cc.p(HeroControlPositionX+4, 3);
            Level.getComponent('LevelControl').SetLocationLine(PlayerCollisionLinePosition);
            // cc.log("出来线吗",PlayerCollisionLinePosition);
        }
        // else{
        //     cc.log(PlayerStateScript.CurrentInZoneType+'+++++++'+HeroControlScript.PlayerStateScript.BStagger);
        //     cc.log(PlayerStateStatic.ZoneType.FrontLongAccelerationBand+'+++++++'+PlayerStateStatic.ZoneType.CenterLongAccelerationBand);
        // }

        if(PlayerStateScript.CurrentInZoneType===PlayerStateStatic.ZoneType.FrontLongAccelerationBand)
        {
            HeroControlScript.SpeedUp(PlayerStateStatic.ColliderSpeedUpPoint.FrontLongAccelerationBand);

            PlayerStateScript.BTouchedScreenInTheZone = true;

            var self = this;
            PlayerStateScript.StayInFrontLongAccelerationBand = function()
            {
                self.CanCreateYellowArea = true;
                self.FillLongAccelerationArea();
            }
            PlayerStateScript.schedule(PlayerStateScript.StayInFrontLongAccelerationBand, 0.01, cc.REPEAT_FOREVER);
        }
        else if(PlayerStateScript.CurrentInZoneType===PlayerStateStatic.ZoneType.CenterLongAccelerationBand)
        {
            PlayerStateScript.BDrawYellowLine = !PlayerStateScript.BDrawYellowLine;
            PlayerStateScript.BTouchedScreenInTheZone = true;

            var self = this;
            PlayerStateScript.StayInFrontLongAccelerationBand = function()
            {
                self.CanCreateYellowArea = true;
                self.FillLongAccelerationArea();
            }
            PlayerStateScript.schedule(PlayerStateScript.StayInFrontLongAccelerationBand, 0.01, cc.REPEAT_FOREVER);
        }
        else if(PlayerStateScript.CurrentInZoneType===PlayerStateStatic.ZoneType.BehindLongAccelerationBand)
        {
            HeroControlScript.Stagger();
        }
    },
    TOUCH_END:function()
    {
        var PlayerStateScript = cc.find('Hero').getComponent("PlayerState");

        if(PlayerStateScript.CurrentInZoneType===PlayerStateStatic.ZoneType.CenterLongAccelerationBand||
            PlayerStateScript.CurrentInZoneType===PlayerStateStatic.ZoneType.FrontLongAccelerationBand)
        {
            cc.find('Hero').getComponent('HeroControl').stopAudioByName("LongAccelerationStart");
            cc.find('Hero').getComponent('HeroControl').playAudioByName("LongAccelerationFinish");
            this.scheduleOnce(function() {
                this.IsPlayingAudio = false;
            }, 0.287);
            PlayerStateScript.unschedule(PlayerStateScript.StayInFrontLongAccelerationBand);
            PlayerStateScript.StayInFrontLongAccelerationBand = null;
            this.CanCreateYellowArea = false;
            //cc.log("*************************************************************************************************");
        }
        this.YellowLinePrefab = null;
        this.HeroOldPositionX = 0;
    },
    FillLongAccelerationArea:function() //这个大概18-25毫秒执行一次
    {
        // cc.log("");
        // cc.log(this.CanCreateYellowArea+'*********');
        if(!this.CanCreateYellowArea)return;
        var HeroControlScript = cc.find('Hero').getComponent("HeroControl");
        var PlayerStateScript = cc.find('Hero').getComponent("PlayerState");

        if (cc.find("Shade").getComponent('Shade').FadeTag) {
            return;
        }
        if(PlayerStateScript.CurrentInZoneType===PlayerStateStatic.ZoneType.BehindLongAccelerationBand)
        {
            PlayerStateScript.unschedule(PlayerStateScript.StayInFrontLongAccelerationBand);
            HeroControlScript.Stagger();
            cc.find('Hero').getComponent('HeroControl').stopAudioByName("LongAccelerationStart");
            cc.find('Hero').getComponent('HeroControl').playAudioByName("LongAccelerationFinish");
            this.IsPlayingAudio = false;
            return;
        }
        if(this.IsPlayingAudio == false){
            this.IsPlayingAudio = true;
            cc.find('Hero').getComponent('HeroControl').playAudioByName("LongAccelerationStart");
        }
        // var HeroControlPosition = HeroControlScript.node.getPosition();
        // cc.log(this.HeroOldPositionX+"==HeroOldPositionX");
        if(this.HeroOldPositionX == 0){
            this.HeroOldPositionX = HeroControlScript.node.x;
            return;
        }
        var SubPositionX = HeroControlScript.node.x - this.HeroOldPositionX;
        var YellowLinePosition = cc.p(HeroControlScript.node.x+50,HeroControlScript.node.y-641-55);
        // var YellowLinePosition = cc.p(HeroControlScript.node.x,HeroControlScript.node.y-641);
        if(this.YellowLinePrefab == null){
            this.YellowLinePrefab = cc.find('Level').getComponent('LevelControl').CreateYellowBG(YellowLinePosition);
        }
        if (this.YellowLinePrefab.getChildByName('Mask') !=null) {
            if (this.YellowLinePrefab.getChildByName('Mask').getChildByName('YellowBG')!=null) {
                this.YellowLinePrefab.getChildByName('Mask').getChildByName('YellowBG').x+=SubPositionX;
            }
        }
        this.HeroOldPositionX = HeroControlScript.node.x;


        HeroControlScript.SpeedUp(PlayerStateStatic.ColliderSpeedUpPoint.CenterLongAccelerationBand);

        // cc.log("我在加速带上面 我 设置了什么 type",HeroControlScript.CharactorScript.type);
        var NetworkSocket = cc.find('Hero').getComponent('NetworkSocket');
        var data = { FunctionName: 'LongAccelerationBandSpeedUp'};
        NetworkSocket.sendSyncData(JSON.stringify(data));


    },
    PrintCurrentState:function(PlayerStateScript)
    {
        switch (PlayerStateScript.CurrentInZoneType)
        {
            case 0 :
                cc.log('None');
                break;
            case 1 :
                cc.log('FrontAccelerationBand');
                break;
            case 2 :
                cc.log('AccelerationBandCenter');
                break;
            case 3 :
                cc.log('BehindAccelerationBand');
                break;
            case 4 :
                cc.log('PreHurdleBand');
                break;
            case 5 :
                cc.log('PerfectHurdleBand');
                break;
            case 6 :
                cc.log('FrontLongAccelerationBand');
                break;
            case 7 :
                cc.log('CenterLongAccelerationBand');
                break;
            case 8 :
                cc.log('BehindLongAccelerationBand');
                break;
            case 9 :
                cc.log('FrontLongJumpBand');
                break;
            case 10 :
                cc.log('PerfectLongJumpBand');
                break;
        }
    },
    update:function(dt)
    {
        // cc.log('***'+cc.find('Hero').getComponent("HeroControl").PlayerStateScript.BStagger);
        var Rivel = cc.find('Rivel');
        var Hero = cc.find('Hero');
        // cc.log(Math.abs(Rivel.position.x-Hero.position.x));
        // if(Math.abs(Rivel.position.x-Hero.position.x)>3000 && this.distanceX<3000)
        // {
        //     cc.log('distance>3000')
        // }
        //
        // if(Math.abs(Rivel.position.x-Hero.position.x)<3000 && this.distanceX>3000)
        // {
        //     cc.log('distance<3000')
        // }

        // this.distanceX=Math.abs(Rivel.position.x-Hero.position.x);
    }
});
//todo 多点触碰 制裁
cc.Node.maxTouchNum = 1;
cc.Node.touchNum = 0;
var __dispatchEvent__ = cc.Node.prototype.dispatchEvent;
cc.Node.prototype.dispatchEvent = function (event) {
    switch (event.type) {
        case 'touchstart':
            if (cc.Node.touchNum < cc.Node.maxTouchNum) {
                cc.Node.touchNum++;
                this._canTouch = true;
                __dispatchEvent__.call(this, event);
            }
            break;
        case 'touchmove':
            if (!this._canTouch && cc.Node.touchNum < cc.Node.maxTouchNum) {
                this._canTouch = true;
                cc.Node.touchNum++;
            }

            if (this._canTouch) {
                __dispatchEvent__.call(this, event);
            }

            break;
        case 'touchend':
            if (this._canTouch) {
                this._canTouch = false;
                cc.Node.touchNum--;
                __dispatchEvent__.call(this, event);
            }
            break;
        case 'touchcancel':
            if (this._canTouch) {
                this._canTouch = false;
                cc.Node.touchNum--;
                __dispatchEvent__.call(this, event);
            }
            break;
        default:
            __dispatchEvent__.call(this, event);
    }
};
