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

    properties: {
        gameStates: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        description: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        countDownlabel: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        smallWhiteRing: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        title: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        videoController: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        demostration: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        bigRing: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        countDownLabel: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        jumpTo: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        bCountDown:false,
        bLoadRes:false,
    },
    onLoad()
    {
        this.audioNames = [
            '1、前绕肩',
            '2、后绕肩',
            '3、屈膝转体热身',
            '4、体前曲出拳',
            '5、拳击站架',
            '6、前直拳',
            '7、后直拳',
            '8、前后滑步',
            '9、前后滑步前直拳',
            '10、上步左右直拳',
            '11、侧闪',
            '12、前摆拳',
            '13、后勾拳',
            '14、前直后勾组合拳',
            '15、前摆后直组合拳',
            '16、直勾摆组合拳',
            '17、摇臂',
            '18、侧闪后直拳',
            '19、交替出拳',
            '20、右侧肩部拉伸',
            '21、左侧肩部拉伸',
            '22、左臂拉伸',
            '23、右臂拉伸',
            '24、最后一个动作，坚持就是胜利'
        ];
        this.gStatesScp = this.gameStates.getComponent('GameStates');
        this.progressBarScp = this.smallWhiteRing.getComponent('ProgressBar');
        this.videoControllerScp = this.videoController.getComponent('SpriteVideoPlayer');
        this.audioControllerScp = this.node.getComponent('AudioController');
        this.bigRingBtn = this.bigRing.getComponent(cc.Button);
        this.bPress = true;
        this.reset();
    },
    reset()
    {
        this.round = gameConfig.rest[this.gStatesScp.currentRound];
        this.description.getComponent(cc.Label).string = this.round.description;
        this.totalTime = this.round.second;
        this.countDownlabel.getComponent(cc.Label).string = this.totalTime.toString();
        this.title.getComponent(cc.Label).string = this.round.name;

        this.gStatesScp.currentSoundIndex = 0;

        this.bLoadRes = false;
        this.bCountDown = false;
        //button
        this.bPress = true;
        this.bigRing.getComponent(cc.Button).interactable = true;

        this.countDownLabel.y = 0;
        this.jumpTo.active = false;
        this.bigRingBtn.interactable = false;
    },
    fire(callback)
    {
        this.reset();
        if(this.gStatesScp.bSinglePlayMode)
        {
            this.audioControllerScp.play('Audios/Game/Names/'+this.audioNames[this.gStatesScp.currentRound]);
        }
        //for testing
        if(gameConfig.rest[this.gStatesScp.currentRound].rest == 1) {
            this.bCountDown = true;
            this.onFinishAllCallback();
            return;
        }

        // console.log('当前资源加载完成1');
        this.videoControllerScp.prepareCurrentRoundVideo(this.gStatesScp.currentRound,()=>{
            // console.log('当前资源加载完成2');
            this.bLoadRes = true;
            if(this.bCountDown)
            {
                this.onFinishAllCallback();
            }
            //如果单独播放模式，加载完视频直接跳转
            if(this.gStatesScp.bSinglePlayMode)
            {
                this.onFinishAllCallback();
                return;
            }
            this.countDownLabel.y = 9;
            this.jumpTo.active = true;
            this.bigRingBtn.interactable = true;
        });

        this.playSound();
        this.progressBarScp.fire(this.totalTime);
        this.callback = callback;
        this.schedule(this.countDown, 1);
    },
    //全部操作完成调用
    onFinishAllCallback(){
        //Multiple press button
        if(!this.bPress) return;
        this.bPress = false;
        this.bigRing.getComponent(cc.Button).interactable = false;

        this.stop();
        this.scheduleOnce(function ()
        {
            this.callback();
            // this.playRound();
            // cc.tween(this.node)
            //     .to(0.5, { opacity: 0})
            //     .call(() => {
            //         this.node.opacity = 255;
            //         this.node.active = false;
            //         this.callback();
            //     })
            //     .start();
        },1);
    },
    playSound()
    {
        if(gameConfig.round[this.gStatesScp.currentRound].second == 2) return;

        if( this.gStatesScp.curretState != this.gStatesScp.demostration) return;

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

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

        if (!currentAudioObj) {
            return;
        }

        let currentAudioPathStr = "Audios/Game/"+currentAudioObj.name;

        let Self = this;

        this.scheduleOnce(function ()
        {
            this.audioControllerScp.playAudio(currentAudioPathStr,function () {
                Self.gStatesScp.currentSoundIndex++;
                Self.playSound();
            });
        },currentAudioObj.delay);
    },
    countDown()
    {
        if(this.totalTime==1)
        {
            this.unschedule(this.countDown);

            this.bCountDown = true;

            if(this.bLoadRes)
            {
                this.onFinishAllCallback();
            }

            return;
        }
        this.totalTime--;
        this.countDownlabel.getComponent(cc.Label).string = this.totalTime.toString();
    },
    stop()
    {
        this.progressBarScp.stop();
    }
});
