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

    properties: {
        gameStates: {
            default: null,
            type: cc.Node, 
            serializable: true,
        },
        totalTime: {
            default: null,
            type: cc.Node, 
            serializable: true,
        },
        playButton: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        kCal: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        resultPlanNode: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    start () {
        //init node
        this.gameStatesScpt = this.gameStates.getComponent('GameStates');
        this.totalTimeLabel = this.totalTime.getComponent(cc.Label);
        this.resultScpt = this.resultPlanNode.getComponent('Result');
        this.countingToShowKcal = 0;
    },
    fireTick()
    {
        this.schedule(this.tick, 1);
    },
    tick()
    {
        //total time
        this.gameStatesScpt.currentTime++;
        this.totalTimeLabel.string = library.formatSeconds(this.gameStatesScpt.currentTime);
        //kcal
        this.gameStatesScpt.kCal+=300/3600;
        this.countingToShowKcal++;

        if(this.countingToShowKcal>10)
        {
            this.countingToShowKcal = 0;
            this.kCal.getComponent(cc.Label).string = this.gameStatesScpt.kCal.toFixed(2).toString();
        }
    },
    untick()
    {
        this.unschedule(this.tick);
    },
    //结束
    playOver(){
        this.resultPlanNode.active = true;
        this.resultScpt.onEnd({
            hit:this.gameStatesScpt.hitCount,
            miss:this.gameStatesScpt.missCount,
            time: Math.round((this.gameStatesScpt.currentTime/60)*100)/100,
            kCal: Math.round(this.gameStatesScpt.kCal*100)/100
        });
    },
    onPlay()
    {
        if(this.bPaused)
        {

            this.playButton.getChildByName('Pause').active = true;
            this.playButton.getChildByName('Play').active = false;
            this.resume();
        }
        else
        {
            this.playButton.getChildByName('Pause').active = false;
            this.playButton.getChildByName('Play').active = true;
            this.pause();
        }
        this.bPaused = !this.bPaused;
    },
    pause()
    {
        cc.director.pause()
    },
    resume()
    {
        cc.director.resume();
    },
    onReplay()
    {
        cc.director.loadScene('Game');
    }
});
