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

    properties: {
        gameStates: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        totalTime: {
            default: null,
            type: cc.Label,
            serializable: true,
        },
        timeProgress: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        countDown: {
            default: null,
            type: cc.Label,
            serializable: true,
        },
    },
    start () {
        this.init();
        this.fireTick();
    },
    init()
    {
        //init node
        this.gameStatesScpt = this.gameStates.getComponent('GameStates');

        this.totalTime.string = library.formatSeconds(this.gameStatesScpt.currentTime);
        this.timeProgressScpt = this.timeProgress.getComponent('ProgressBar');
        this.countDownNum = 3;

        this.gameStatesScpt.curretState = this.gameStatesScpt.playing;
        this.lastAppearMouse = null;
    },
    fireTick()
    {
        this.schedule(this.tick, 1);
    },
    tick()
    {
        if(this.gameStatesScpt.currentTime==0)
        {
            this.untick();
            this.gameOver();
            return;
        }
        //total time
        this.gameStatesScpt.currentTime--;
        this.totalTime.string = library.formatSeconds(this.gameStatesScpt.currentTime);
        this.timeProgressScpt.setValue(1-(this.gameStatesScpt.currentTime/gameConfig.roundTime));

        this.checkCurrentLV();

        //countdown
        if(this.countDownNum<0) return;

        if(this.countDownNum==0)
        {
            this.countDown.string = '';
            this.startComeOutMouse();
        }
        else
        {
            this.countDown.string = this.countDownNum.toString();
        }

        this.countDownNum--;

    },
    untick()
    {
        this.unschedule(this.tick);
    },
    checkCurrentLV()
    {
        let c_percentage = (1-this.gameStatesScpt.currentTime/gameConfig.roundTime)*100;
        let percentage = gameConfig.comeOutAiInLv[this.gameStatesScpt.currentLv].percentage;

        if(c_percentage>=percentage &&
            this.gameStatesScpt.currentLv<gameConfig.comeOutAiInLv.length-1)
        {
            this.gameStatesScpt.currentLv++;
        }
    },
    startComeOutMouse()
    {
        let self = this;
        let lv = gameConfig.comeOutAiInLv;
        let gStates = this.gameStatesScpt;

        //结束后关闭计时器
        if(gStates.curretState == gStates.finished)
        {
            self.unschedule(this.startComeOutMouse);
            return;
        }

        let appearDur = lv[gStates.currentLv].appearDur;
        let num = lv[gStates.currentLv].num;
        //如果数组里面的数量小于num 就不要生成那么多地鼠
        if(num>gStates.hiddenMouseArr.length)
        {
            num = gStates.hiddenMouseArr.length;
        }
        //随机1-num 出地鼠保证出地鼠不是看起来都是 同样的数量 好看
        if(gStates.hiddenMouseArr.length!=0)
        {
            num = library.randomInt(1,num);
        }
        //创建地鼠
        for(let i=0;i<num;i++)
        {
            let index = library.randomInt(0,gStates.hiddenMouseArr.length-1);
            let aMouse = gStates.hiddenMouseArr[index];

            if(self.lastAppearMouse == aMouse)
            {
                if(index+1>=gStates.hiddenMouseArr.length)
                {
                    index = 0;
                }
                else
                {
                    aMouse = gStates.hiddenMouseArr[index+1];
                }
            }

            self.lastAppearMouse = aMouse;

            let aMouseScp = aMouse.getComponent('Actor');

            aMouseScp.spawn(aMouse,appearDur,function (aMouse) {

                aMouseScp.die();

                if(i==num-1)
                {
                    self.scheduleOnce(function () {
                        self.startComeOutMouse();
                    },lv[gStates.currentLv].duration);
                }
            });
        }
    },
    gameOver()
    {
        this.gameStatesScpt.curretState = this.gameStatesScpt.finished;
    }
});
