import { Widget, _decorator, Node } from 'cc';
import { UIBase } from '../scriptBase/UIBase';
import { GameMgr } from '../manager/GameMgr';
import { Switcher } from '../uiExtend/Switcher';
import { Setting } from '../Setting';
import { AudioMgr } from '../manager/AudioMgr';
import { TransMgr } from '../manager/TransMgr';
import { UI } from '../enum/UI';
const { ccclass, property, requireComponent } = _decorator;

@ccclass('UI/UIPause')
@requireComponent(Widget)
export class UIPause extends UIBase {

    private swBgm: Switcher = null
    private swSfx: Switcher = null

    protected onLoad(): void {
        this.swBgm = this.findComp('SwBgm', Switcher)
        this.swSfx = this.findComp('SwSfx', Switcher)
    }

    public onOpen(data?: any): void {
        GameMgr.Pause = true

        this.swBgm.IsOn = Setting.BgmEnabled
        this.swSfx.IsOn = Setting.SfxEnabled
    }

    public onClose(data?: any): void {
        GameMgr.Pause = false
    }

    protected onBtnCloseClick(): void {
        this.close()
    }

    protected async onBtnRestartClick() {
        await TransMgr.Inst.fadeIn()
        GameMgr.startGame()
        this.close()
        TransMgr.Inst.fadeOut()
    }

    protected async onBtnBackClick() {
        await TransMgr.Inst.fadeIn()
        GameMgr.quitGame()
        this.close()
        this.open(UI.Main)
        TransMgr.Inst.fadeOut()
    }

    protected onSwBgmClick(): void {
        Setting.BgmEnabled = !Setting.BgmEnabled
        if (Setting.BgmEnabled) {
            AudioMgr.playBgm()
        } else {
            AudioMgr.stopBgm()
        }
    }

    protected onSwSfxClick(): void {
        Setting.SfxEnabled = !Setting.SfxEnabled
    }

}


