// 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/GameNS16_ns16_mole";
import pg from "../tool/pg_ns16_mole";
pg.event.clear();
const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

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

    private btn_laba: cc.Node;

    private countDown: number = 999;
    @property
    text: string = 'hello';

    // LIFE-CYCLE CALLBACKS:

    playBgm: boolean;
    onLoad() {
        this.playBgm = true;
        this.initView();
        this.initEvent();
    }

    private initEvent() {
        pg.event.on("show_round_action", () => {
            this.countDown = Game.getIns().getCurrentPage().duration;
            this.showRound();
        });
        pg.event.on("game_start", () => {
            this.gameStart();
        });
        pg.event.on("game_set_game_time", (count) => {
            this.countDown = count;
        });
        pg.event.on("game_play_laba", () => {
            this.playLaba();
        });
        this.btn_laba.on(cc.Node.EventType.TOUCH_END, this.playLaba, this);
        this.btn_music.on(cc.Node.EventType.TOUCH_END, this.onTouchMusic, this);
    }
    private gameStart() {
        if (this.playBgm) {
            pg.audio.playBgmByUrl(Game.getIns().bgm);
        }
        this.countDown = Game.getIns().getCurrentPage().duration;
    }
    private bg_round: cc.Node;
    private btn_music: cc.Node;
    private initView() {
        this.btn_laba = cc.find("btn_laba", this.node);
        this.bg_round = cc.find("bg_round", this.node);
        this.btn_music = pg.view.find(this, 'btn_music');
    }
    private showRound() {
        pg.view.visible(this.bg_round, true);
        let round = Game.getIns().getCurrentPageNum();
        pg.view.setString(pg.view.find(this.bg_round, 'img/desc'), round);
        this.bg_round.x = 1500;
        cc.tween(this.bg_round).to(1, { x: 0 }, { easing: 'quadIn' }).delay(1).to(1, { x: -1500 }, { easing: 'quadOut' })
            .call(() => { pg.event.emit("game_start"); })
            .start();
    }
    onTouchMusic() {
        if (cc.audioEngine.isMusicPlaying()) {
            cc.audioEngine.pauseMusic();
            this.playBgm = false;
            pg.view.visible(pg.view.find(this.btn_music, 'off'), true);
        } else {
            this.playBgm = true;
            cc.audioEngine.resumeMusic();
            pg.audio.playBgmByUrl(Game.getIns().bgm);
            pg.view.visible(pg.view.find(this.btn_music, 'off'), false);
        }
    }

    private intervalId;
    private playing: boolean;
    private playLaba() {
        if (this.playing) return;
        this.playing = true;
        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) {
                this.playing = false;
                clearInterval(this.intervalId);
                pg.event.emit("game_play_laba_successed");
            }
        }, 150);
        pg.audio.playAudioByUrlThen(Game.getIns().getCurrentPage().audio).then(() => {
            stop = true;
        })
    }
    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;
        }
    }
}





