// 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 from "./Game";
import pg from "./pg";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    // LIFE-CYCLE CALLBACKS:
    onLoad() {
        pg.view.touchOn(this.node, this.onTouchLaba, this);
        pg.event.on("play_stem", () => {
            this.onTouchLaba().then(() => {
                pg.event.emit("game_start_loop")
            })
        });
    }
    protected onDestroy(): void {
        this.unschedule(this.labaAni);
        pg.event.off("play_stem");
    }
    onTouchLaba() {
        return new Promise((resolve, reject) => {
            this.showAni();
            pg.audio.playAudioByUrl(Game.getIns().nowAudio).then(() => {
                this.stopAni();
                resolve('');
            })
        });
    }
    private count: number;
    showAni() {
        this.count = 0;
        this.schedule(this.labaAni, 0.1);
    }
    stopAni() {
        this.unschedule(this.labaAni);
        this.count = 2;
        this.labaAni();
    }
    labaAni() {
        this.count++;
        switch (this.count) {
            case 0:
                pg.view.visible(pg.view.find(this, "btn_laba2"), true);
                pg.view.visible(pg.view.find(this, "btn_laba3"), false);
                pg.view.visible(pg.view.find(this, "btn_laba4"), false);
                break;
            case 1:
                pg.view.visible(pg.view.find(this, "btn_laba2"), false);
                pg.view.visible(pg.view.find(this, "btn_laba3"), true);
                pg.view.visible(pg.view.find(this, "btn_laba4"), false);
                break;
            case 2:
                pg.view.visible(pg.view.find(this, "btn_laba2"), false);
                pg.view.visible(pg.view.find(this, "btn_laba3"), false);
                pg.view.visible(pg.view.find(this, "btn_laba4"), true);
                break;
            case 3:
                pg.view.visible(pg.view.find(this, "btn_laba2"), false);
                pg.view.visible(pg.view.find(this, "btn_laba3"), false);
                pg.view.visible(pg.view.find(this, "btn_laba4"), false);
                this.count = -1;
                break;
        }

    }

    // update (dt) {}
}
