import pg from "./pg";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.initView();
        let panel_gray = pg.view.find(this, "panel_gray");
        let btn_start = pg.view.find(this, "btn_start");
        let btn_restart = pg.view.find(this, "btn_restart");
        let bg_vs = pg.view.find(this, "bg_vs");
        pg.view.touchOn(btn_start, this.onTouchStart, this);
        pg.view.touchOn(btn_restart, this.onTouchRestart, this);
        pg.event.on("game_over", () => {
            this.onGameOver();
        })
    }
    initView() {
        let panel_gray = pg.view.find(this, "panel_gray");
        let btn_start = pg.view.find(this, "btn_start");
        let btn_restart = pg.view.find(this, "btn_restart");
        let bg_vs = pg.view.find(this, "bg_vs");
        panel_gray.active = true;
        btn_start.active = true;
    }
    onTouchStart() {
        pg.hw.playLocalAudio("vs");
        let panel_gray = pg.view.find(this, "panel_gray");
        let btn_start = pg.view.find(this, "btn_start");
        let btn_restart = pg.view.find(this, "btn_restart");
        let bg_vs = pg.view.find(this, "bg_vs");
        panel_gray.active = true;
        btn_start.active = false;
        bg_vs.active = true;
        pg.event.emit("game_start");
        setTimeout(() => {
            bg_vs.active = false;
            panel_gray.active = false;
        }, 2000);
    }
    onTouchRestart() {
        pg.hw.playLocalAudio("vs");
        let panel_gray = pg.view.find(this, "panel_gray");
        let btn_start = pg.view.find(this, "btn_start");
        let btn_restart = pg.view.find(this, "btn_restart");
        let bg_vs = pg.view.find(this, "bg_vs");
        panel_gray.active = true;
        btn_start.active = false;
        btn_restart.active = false;
        bg_vs.active = true;
        setTimeout(() => {
            bg_vs.active = false;
            panel_gray.active = false;
            btn_restart.active = false;
            pg.event.emit("game_start");
        }, 2000);
    }
    onGameOver() {
        let panel_gray = pg.view.find(this, "panel_gray");
        let btn_restart = pg.view.find(this, "btn_restart");
        let bg_vs = pg.view.find(this, "bg_vs");
        panel_gray.active = true;
        btn_restart.active = true;
    }
    update(dt) { }
}
