import { _decorator, Button, Node } from 'cc';
import { BasePopUp } from '../../../A-FRAME/component/ui.pop-up';
import { AudioManager } from '../../../AudioManager';
import { Url } from '../../../Url';
import type { Popup } from '../page/popup';
const { ccclass, property } = _decorator;

@ccclass('SettingPage')
export class SettingPage extends BasePopUp<typeof Popup, { type: string, value: number }> {

    protected default_return: { type: string; value: number; };
    protected pageConfig: any = null;

    @property(Node)
    musicOpenBtn: Node = null;
    @property(Node)
    musicCloseBtn: Node = null;

    @property(Node)
    bgmOpenBtn: Node = null;
    @property(Node)
    bgmCloseBtn: Node = null;

    musicIsOpen: boolean = true;
    bgmIsOpen: boolean = true;

    public onShow(config: any): void {
        this.pageConfig = config;
        BasePopUp.jelly_enter(this.node);
        this.renderLocalUI();
    }

    protected _click_event_: { [name: string]: (this: SettingPage, button: Button) => void; } = {
        closeBtn(btn) {
            AudioManager.ins.playOneShot(Url.AUDIO.SFX4, 1);
            btn.interactable = false;
            BasePopUp.jelly_leave(this.node).then(() => {
                this.close(null);
                btn.interactable = true;
            });
        },
        musicBtn(btn) {
            //  音乐开关
            if (this.musicIsOpen) {
                this.musicOpenBtn.active = false;
                this.musicCloseBtn.active = true;
                this.musicIsOpen = false;
                AudioManager.ins.effectMute = true;
            } else {
                this.musicOpenBtn.active = true;
                this.musicCloseBtn.active = false;
                this.musicIsOpen = true;
                AudioManager.ins.effectMute = false;
            }
        },
        bgmBtn(btn) {
            //  背景音乐开关
            if (this.bgmIsOpen) {
                this.bgmOpenBtn.active = false;
                this.bgmCloseBtn.active = true;
                this.bgmIsOpen = false;
                AudioManager.ins.bgmMute = true;
            } else {
                this.bgmOpenBtn.active = true;
                this.bgmCloseBtn.active = false;
                this.bgmIsOpen = true;
                AudioManager.ins.bgmMute = false;
            }
        }
    }

    start() {

    }

    update(deltaTime: number) {

    }

    async renderLocalUI() {

        if (AudioManager.ins.effectMute) {
            // console.log('关闭音效按钮状态');
            this.musicOpenBtn.active = false;
            this.musicCloseBtn.active = true;
            this.musicIsOpen = false;
        } else {
            // console.log('打开音效按钮状态');
            this.musicOpenBtn.active = true;
            this.musicCloseBtn.active = false;
            this.musicIsOpen = true;
        }

        if (AudioManager.ins.bgmMute) {
            // console.log('关闭bgm按钮状态');
            this.bgmOpenBtn.active = false;
            this.bgmCloseBtn.active = true;
            this.bgmIsOpen = false;
        } else {
            // console.log('打开bgm按钮状态');
            this.bgmOpenBtn.active = true;
            this.bgmCloseBtn.active = false;
            this.bgmIsOpen = true;
        }
    }

}

