import { _decorator, Component, ERaycast2DType, find, Label, Node, NodeEventType, PhysicsSystem2D, Tween, tween, v2, v3, Vec2, Vec3 } from 'cc';
import { EventDispatcher } from '../core_tgx/easy_ui_framework/EventDispatcher';
import { StormSunderGlobalInstance } from './Script/StormSunderGlobalInstance';
import { GameEvent } from './Script/Enum/GameEvent';
import { LevelManager } from './Script/Manager/LevelMgr';
import { GameUtil } from './Script/GameUtil';

import { tgxUIMgr } from '../core_tgx/tgx';
import { UI_Setting } from '../scripts/UIDef';
import { StormSunderAudioMgr } from './Script/Manager/StormSunderAudioMgr';
const { ccclass, property } = _decorator;

@ccclass('RoosterStormSunder')
export class RoosterStormSunder extends Component {

    onLoad() {
        StormSunderAudioMgr.initilize();
        StormSunderAudioMgr.play(StormSunderAudioMgr.getMusicIdName(1), 1.0);

        LevelManager.instance.initilizeModel();
        StormSunderGlobalInstance.instance.map = find('Canvas/Map')!;

        this.registerListener();
    }

    protected start(): void {
        this.startGame();
    }

    async startGame() {
        // await LevelManager.instance.gameStart();
    }

    registerListener() {
        const btnRefresh = find('Canvas/GameUI/TopLeft/BtnRefresh')!;
        const btnSet = find('Canvas/GameUI/TopLeft/BtnSet')!;
        btnRefresh.on(NodeEventType.TOUCH_END, () => this.onClickRefresh(), this);
        btnSet.on(NodeEventType.TOUCH_END, () => this.onClickSet(), this);
    }

    private async onClickRefresh(): Promise<void> {
        StormSunderAudioMgr.playOneShot(StormSunderAudioMgr.getMusicIdName(2), 1.0);
        Tween.stopAll();
        EventDispatcher.instance.emit(GameEvent.EVENT_BATTLE_FAIL_LEVEL_RESET);
    }

    private onClickSet(): void {
        StormSunderAudioMgr.playOneShot(StormSunderAudioMgr.getMusicIdName(2), 1.0);
        const show = tgxUIMgr.inst.isShowing(UI_Setting);
        if (!show) {
            tgxUIMgr.inst.showUI(UI_Setting);
        }
    }


}




