let gameConfig = require("GameConfig");
let library = require("../Library");
cc.Class({
    extends: cc.Component,

    properties: {
        gameStates: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        gameMode: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        playerController: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        countDown: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        kCal: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        restTimeRing: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        roundCurrentTime: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        roundTxt: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        nameTxt: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        videoController: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        videoplayer: {
            default: null,
            type: cc.Node,
            serializable: true,
        },progressBar: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        comb: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        countDownAudio: {
            default: null,
            type: cc.AudioClip,
            serializable: true,
        },
        startGunAudio: {
            default: null,
            type: cc.AudioClip,
            serializable: true,
        },
        actions: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        hint: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        loopTxt: {
            default: null,
            type: cc.Label,
            serializable: true,
        },
        nextBtn: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    onLoad () {
        this.gStatesScp = this.gameStates.getComponent('GameStates');
        this.gameModeScp = this.gameMode.getComponent('GameMode');
        this.playerControllerScp = this.playerController.getComponent('PlayerController');
        this.restTimeRingScp = this.restTimeRing.getComponent('ProgressBar');
        this.videoControllerScp = this.videoController.getComponent('SpriteVideoPlayer');
        this.audioControllerScp = this.node.getComponent('AudioController');
        this.combScp = this.comb.getComponent('Combo');
        this.punchInteractScp = this.actions.getComponent('PunchInteract');
        this.hintScp = this.hint.getComponent('Hint');

        if(!this.gStatesScp.bLoop)
        {
            this.loopTxt.string = '循环';
        }
        else {
            this.loopTxt.string = '顺序';
        }
    },
    resetRound()
    {
        this.round = gameConfig.round[this.gStatesScp.currentRound];
        this.gStatesScp.curretState = this.gStatesScp.countDown;

        this.gStatesScp.bSecondOrTimes = 0;
        if(this.round.second != 0)
        {
            this.gStatesScp.bSecondOrTimes = 1;
        }

        if(this.gStatesScp.bSecondOrTimes)
        {
            this.roundCurrentTime.getComponent(cc.Label).string = this.round.second+' ″';
        }
        else
        {
            this.roundCurrentTime.getComponent(cc.Label).string = this.round.times+' ″';
        }

        this.roundTxt.getComponent(cc.Label).string =  this.gStatesScp.currentRound+1;
        this.nameTxt.getComponent(cc.Label).string = this.round.name;
        this.restTimeRingScp.getComponent(cc.Sprite).fillRange = 1;

        this.gStatesScp.roundTime = 0;

        this.gStatesScp.currentSoundIndex = 0;

        this.progressBar.getComponent(cc.ProgressBar).progress = (this.gStatesScp.currentRound+1)/24;
        if(this.gStatesScp.currentRound+1==24)
        {
            this.progressBar.getComponent(cc.ProgressBar).progress = 1;
        }

        for(let i=0; i<this.gStatesScp.hightLightActionArr.length;i++)
        {
            if(this.gStatesScp.hightLightActionArr[i]&&this.gStatesScp.hightLightActionArr[i].node)
                this.gStatesScp.hightLightActionArr[i].node.destroy();
        }

        this.gStatesScp.hightLightActionArr.length = 0;

        this.nextBtn.getComponent(cc.Button).interactable = true;
    },
    // startPlay(callback)
    startPlay()
    {
        let self = this;
        // this.callback = callback;
        this.gStatesScp.curretState = self.gStatesScp.countDown;
        this.playVideo();
        this.showActionBar();
        let times = 3;

        let countDown = function()
        {
            if(times==0)
            {
                this.unschedule(countDown);
                this.scheduleOnce(function () {

                    self.audioControllerScp.playAudio('Audios/Game/Others/3、GO',function () {

                        self.gStatesScp.curretState = self.gStatesScp.playing;
                        self.countDown.getComponent(cc.Label).string = '';
                        self.playSound();
                        //start
                        self.playRoundBySecond();
                    });
                },0.2);

                return;
            }
            times--;
            this.countDown.getComponent(cc.Label).string = (times+1).toString();
            this.audioControllerScp.play('Audios/Game/Nums/'+(times+1).toString());
        };
        this.schedule(countDown, 1);
    },
    playSound()
    {
        let self = this;

        if(this.gStatesScp.currentSoundIndex == gameConfig.audioRound[this.gStatesScp.currentRound].length ||
            gameConfig.audioRound[this.gStatesScp.currentRound].length == 0
        )
        {
            return;
        }

        let currentAudioObj = gameConfig.audioRound[this.gStatesScp.currentRound][this.gStatesScp.currentSoundIndex];

        if (!currentAudioObj) return;

        let currentAudioPathStr = "Audios/Game/"+currentAudioObj.name;
        // console.log('44444=',currentAudioObj.name);
        // console.log('xxxx=',currentAudioObj.delay);
        this.scheduleOnce(function ()
        {
            self.audioControllerScp.playAudio(currentAudioPathStr,function () {
                self.gStatesScp.currentSoundIndex++;
                self.playSound();
            });
            // console.log('44444=',currentAudioPathStr);
            // console.log('3333=',this.gStatesScp.currentRound);
        },currentAudioObj.delay);
    },
    showActionBar()
    {
        let actionPlay = gameConfig.actionPlay[this.gStatesScp.currentRound];
        if(actionPlay.length == 0)
        {
            this.actions.parent.getChildByName('BG').y = -100;
            this.actions.active = false;
        }
        else {
            this.actions.parent.getChildByName('BG').y = 0;
            this.actions.active = true;
        }
    },
    playVideo()
    {
        this.videoControllerScp.play(this.videoplayer,this.gStatesScp.currentRound,this,this.videoCompleted);
    },
    videoCompleted(self)
    {
        if(self.gStatesScp.curretState == self.gStatesScp.countDown)
        {
            self.videoControllerScp.play(self.videoplayer,self.gStatesScp.currentRound,self,self.videoCompleted);
        }else if(self.gStatesScp.curretState == self.gStatesScp.playing)
        {
            self.videoControllerScp.play(self.videoplayer,self.gStatesScp.currentRound,self,self.videoCompleted);
            self.punchInteractScp.playActions();
        }
    },
    playRoundBySecond()
    {
        //start
        let self = this;
        this.gameModeScp.fireTick();
        this.restTimeRingScp.fire(this.round.second);

        this.timer = function ()
        {
            if (this.gStatesScp.roundTime == this.round.second)
            {
                this.finished();
                return;
            }

            if(this.round.second - this.gStatesScp.roundTime == 7)
            {
                self.audioControllerScp.play('Audios/Game/Others/再坚持5秒');
            }

            this.gStatesScp.roundTime++;
            this.roundCurrentTime.getComponent(cc.Label).string = (this.gStatesScp.roundTime).toString()+' ″';
            cc.audioEngine.play(this.countDownAudio, false, 0.5);
        };
        this.schedule(this.timer, 1);
    },
    finished()
    {
        let self = this;
        this.stop();
        cc.audioEngine.play(this.startGunAudio, false, 1);
        // roundFinished
        this.gStatesScp.curretState = this.gStatesScp.default;

        if(this.gStatesScp.bLoop)
        {
            this.playerControllerScp.playRound();
            return;
        }

        this.gStatesScp.currentRound++;
        if(this.gStatesScp.currentRound == gameConfig.round.length-1)
        {
            this.gStatesScp.curretState = this.gStatesScp.default;
            //播放结束后
            this.gameModeScp.playOver();
            return;
        }

        //delay to start
        this.playerControllerScp.scheduleOnce(function()
        {
            self.playerControllerScp.rest.x += self.playerControllerScp.rest.width;
            self.playerControllerScp.rest.active = true;
            self.playerControllerScp.rest.opacity = 255;
            self.playerControllerScp.playRest();

            cc.tween(self.playerControllerScp.rest)
                .by(0.5, { position: cc.v2(-self.playerControllerScp.rest.width, 0) })
                .start()
        },1);
    },
    next(event, customEventData)
    {
        let self = this;
        //press Onetime
        let node = event.target;
        node.getComponent(cc.Button).interactable = false;

        this.stop();
        cc.audioEngine.play(this.startGunAudio, false, 1);
        // roundFinished
        this.gStatesScp.curretState = this.gStatesScp.default;
        this.gStatesScp.currentRound++;
        if(this.gStatesScp.currentRound == gameConfig.round.length-1)
        {
            this.gStatesScp.curretState = this.gStatesScp.default;
            //播放结束后
            this.gameModeScp.playOver();
            return;
        }

        //delay to start
        this.playerControllerScp.scheduleOnce(function()
        {
            self.playerControllerScp.rest.x += self.playerControllerScp.rest.width;
            self.playerControllerScp.rest.active = true;
            self.playerControllerScp.rest.opacity = 255;
            self.playerControllerScp.playRest();

            cc.tween(self.playerControllerScp.rest)
                .by(0.5, { position: cc.v2(-self.playerControllerScp.rest.width, 0) })
                .start()
        },1);
    },
    playMode(event, customEventData)
    {
        if(!this.gStatesScp.bLoop)
        {
            this.loopTxt.string = '顺序';
            this.hintScp.showHintStr('已切换到循环播放');
        }
        else {
            this.loopTxt.string = '循环';
            this.hintScp.showHintStr('已切换到顺序播放');
        }

        this.gStatesScp.bLoop = !this.gStatesScp.bLoop;
    },
    stop()
    {
        // this.unscheduleAllCallbacks();
        this.unschedule(this.timer);
        this.gameModeScp.untick();
        this.videoControllerScp.stop();
    }
});
