// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Game, { GAME_STATE } from "../tool/Game";
import pg from "../tool/pg";
pg.event.clear();
const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    // private btn_laba: cc.Node;

    // private countDown: number = 999;

    onLoad() {
        this.initView();
        this.initEvent();
    }

    private initEvent() {
        pg.event.on("game_start", () => {
            this.gameStart();
        });
        // pg.event.on("game_set_game_time", (count) => {
        //     this.countDown = count;
        // });
        // this.btn_laba.on(cc.Node.EventType.TOUCH_END, this.playLaba, this);
    }
    private gameStart() {
        // this.countDown = Game.getIns().getCurrentPage().duration;
        // this.playLaba();
    }
    private initView() {
        // this.btn_laba = cc.find("btn_laba", this.node);
    }

    // private intervalId;
    // private playLaba() {
    //     let btn_kaba2 = this.btn_laba.getChildByName("btn_laba2");
    //     let btn_kaba3 = this.btn_laba.getChildByName("btn_laba3");
    //     btn_kaba2.active = true;
    //     btn_kaba3.active = true;

    //     let count = 0;
    //     if (this.intervalId) clearInterval(this.intervalId);
    //     let stop = false;
    //     this.intervalId = setInterval(() => {
    //         count++;
    //         btn_kaba2.active = count % 3 == 1;
    //         btn_kaba3.active = count % 3 == 2;
    //         if (stop && count % 3 == 2) clearInterval(this.intervalId);
    //     }, 150)

    //     pg.audio.playAudioByUrl(Game.getIns().getCurrentPage().audio).then(() => { stop = true; })
    // }
    // private lastCount: number;
    update(dt) {
        if (Game.getIns().title) {
            cc.find("bg_title/title", this.node).getComponent(cc.Label).string = Game.getIns().title;
        }
        // if (Game.getIns().state == GAME_STATE.OVER) return;
        // if (Game.getIns().state == GAME_STATE.WAIT) return;
        // if (this.countDown <= 0) {
        //     Game.getIns().state = GAME_STATE.OVER;
        //     pg.event.emit("game_time_over");
        //     return;
        // }
        // this.countDown = this.countDown - dt;
        // if (this.countDown < 6) {
        //     let count = Math.floor(this.countDown);
        //     if (count != this.lastCount) {
        //         this.lastCount = count;
        //         if (this.lastCount == 1) {
        //             pg.audio.playLocalAudio(cc.find("bg_cd_red/audio_cd1", this.node))
        //         } else if (this.lastCount > 1) {
        //             pg.audio.playLocalAudio(cc.find("bg_cd_red/audio_cd", this.node))
        //         }
        //     }
        //     cc.find("bg_cd_red", this.node).active = true;
        //     cc.find("bg_cd_red/label_cd", this.node).getComponent(cc.Label).string = "" + parseInt("" + this.countDown);
        // } else {
        //     cc.find("bg_cd_red", this.node).active = false;
        //     cc.find("bg_cd/label_cd", this.node).getComponent(cc.Label).string = "" + parseInt("" + this.countDown);
        // }
    }
}