/**
 * 代码描述
 */

import { Audio } from "../../common/src/Audio";
import { Store } from "../../common/src/Store";
import { UIManager } from "../../common/src/UIManager";
import { Utils } from "../../common/src/Utils";


const { ccclass, property } = cc._decorator;

@ccclass
export class PausePanel extends cc.Component {

    @property({ type: cc.Node, tooltip: '按钮节点1' })
    button1: cc.Node = null;

    @property({ type: cc.Node, tooltip: '按钮节点2' })
    button2: cc.Node = null;

    private enabledMusic: boolean = true;
    private enabledEffect: boolean = true;

    onEnable() {
        this.node.getChildByName('panel').getChildByName('closeButton').on('click', this.onClose, this);
        this.button1.getChildByName('musicEffect').on('click', this.onMusicEffect, this);
        this.button1.getChildByName('music').on('click', this.onMusic, this);
        this.button2.getChildByName('restart').on('click', this.onRestart, this);
        this.button2.getChildByName('quit').on('click', this.onQuit, this);

        this.enabledMusic = Store.getBool('enabledMusic', true);
        this.enabledEffect = Store.getBool('enabledEffect', true);

        this.node.getChildByName('panel').scale = 0.7;
        Utils.openPanel(this.node.getChildByName('panel'));
        this.switchMask();
    }

    /**关闭 */
    private onClose() {
        Audio.playSoundByPath('pause:res/snd/click');

        UIManager.close('pause:PausePanel', true);
    }

    /**音效*/
    private onMusicEffect(): void {
        Audio.playSoundByPath('pause:res/snd/click');
        if (this.enabledEffect) {
            this.enabledEffect = false;
        }
        else {
            this.enabledEffect = true;
        }
        Audio.enableSound(this.enabledEffect);
        this.button1.getChildByName('musicEffect').getChildByName('mask').active = !this.enabledEffect;
    }

    /**音乐 */
    private onMusic(): void {
        Audio.playSoundByPath('pause:res/snd/click');
        if (this.enabledMusic) {
            this.enabledMusic = false;
        }
        else {
            this.enabledMusic = true;
        }
        Audio.enableMusic(this.enabledMusic);
        this.button1.getChildByName('music').getChildByName('mask').active = !this.enabledMusic;

    }

    /**重开 */
    private onRestart(): void {
        Audio.playSoundByPath('pause:res/snd/click');
        UIManager.close('pause:PausePanel', true);
        cc.systemEvent.emit('restart');
    }

    /**退出 */
    private onQuit(): void {
        Audio.playSoundByPath('pause:res/snd/click');
    }

    private switchMask(): void {
        this.button1.getChildByName('musicEffect').getChildByName('mask').active = !this.enabledEffect;
        this.button1.getChildByName('music').getChildByName('mask').active = !this.enabledMusic;
    }
}
