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

    properties: {
        gameStates: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        readyGo: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        totalTime: {
            default: null,
            type: cc.Label,
            serializable: true,
        },
        timeProgress: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        scareCrow: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        result: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    start () {
        this.init();
        this.armatureDisplay.playAnimation("animation",1);
    },
    init()
    {
        //init node
        this.gStatesScp = this.gameStates.getComponent('GameStates');
        this.audioPContrScp = this.gameStates.getComponent('AudioController');
        this.armatureDisplay = this.readyGo.getComponent(dragonBones.ArmatureDisplay);
        //获取 Armatrue
        this.armature = this.armatureDisplay.armature();
        //添加动画监听
        this.armatureDisplay.addEventListener(dragonBones.EventObject.COMPLETE, this.animationEventHandler, this)

        this.totalTime.string = library.formatSeconds(this.gStatesScp.currentTime);
        this.timeProgressScp = this.timeProgress.getComponent('ProgressBar');

        this.gStatesScp.curretState = this.gStatesScp.playing;
        this.lastAppearMouse = null;
    },
    animationEventHandler(event)
    {
        let self = this;
        if (event.type === dragonBones.EventObject.COMPLETE)
        {
            if (event.animationState.name === "animation")
            {
                this.getComponent(cc.AudioSource).play();
                this.fireTick();
                //come in
                cc.tween(self.scareCrow)
                    .by(0.1, { position: cc.v2(self.scareCrow.width,self.scareCrow.y)})
                    .start()
                this.scheduleOnce(function () {
                    this.startComeOutMouse();
                },2)
            }
        }
    },
    fireTick()
    {
        this.schedule(this.tick, 1);
    },
    tick()
    {
        if(this.gStatesScp.currentTime==0)
        {
            this.untick();
            this.gameOver();
            return;
        }
        //total time
        this.gStatesScp.currentTime--;
        this.totalTime.string = library.formatSeconds(this.gStatesScp.currentTime);
        this.timeProgressScp.setValue(1-(this.gStatesScp.currentTime/gameConfig.roundTime));

        this.checkCurrentLV();
    },
    untick()
    {
        this.unschedule(this.tick);
    },
    checkCurrentLV()
    {
        let c_percentage = (1-this.gStatesScp.currentTime/gameConfig.roundTime)*100;
        let percentage = gameConfig.comeOutAiInLv[this.gStatesScp.currentLv].percentage;
        // console.log('00000=',c_percentage)
        // console.log('11111=',percentage)
        if(c_percentage>=percentage &&
            this.gStatesScp.currentLv<gameConfig.comeOutAiInLv.length-1)
        {
            this.gStatesScp.currentLv++;
        }
    },
    startComeOutMouse()
    {
        let self = this;
        let lv = gameConfig.comeOutAiInLv;
        let gStates = this.gStatesScp;

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

        let appearDur = lv[gStates.currentLv].appearDur;
        let num = lv[gStates.currentLv].num;
        // console.log('111=',gStates.hiddenMouseArr.length);
        // console.log('num=',num);
        //如果数组里面的数量小于num 就不要生成那么多地鼠
        if(num>gStates.hiddenMouseArr.length)
        {
            num = gStates.hiddenMouseArr.length;
        }

        //随机1-num 出地鼠保证出地鼠不是看起来都是 同样的数量 好看
        if(gStates.hiddenMouseArr.length!=0)
        {
            num = library.randomInt(1,num);
        }
        // console.log('num33=',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(appearDur);
        }
        // next turn
        let nextTurnDur = lv[gStates.currentLv].appearDur+lv[gStates.currentLv].duration+0.1+0.1;
        // console.log('1111=',lv[gStates.currentLv].appearDur)
        // console.log('1111=',lv[gStates.currentLv].duration)
        self.scheduleOnce(function () {
            self.startComeOutMouse();
        },nextTurnDur);
    },
    gameOver()
    {
        this.untick();
        this.result.active = true;
        let resultScp = this.result.getComponent('Result');
        resultScp.setResult();

        this.gStatesScp.curretState = this.gStatesScp.finished;
    },
    replay()
    {
        // cc.director.loadScene("Pairing");
        cc.director.loadScene("Game");
    },
});
