import { _decorator, Button, Component, Label, Node } from 'cc';
import { UIPanel } from '../base/UIPanel';
import { AudioSettingsData, SettingsData } from '../../Managers/data/DataModel';
import { GameSettingsManager } from '../../Managers/global/data-flow/settings/GameSettingsManager';
import { ManagerType } from '../../Managers/global/data-flow/TopLevelManager';
import { PopupType } from '../popup/PopupType';
const { ccclass, property } = _decorator;

@ccclass('MenuSettingsPanel')
export class MenuSettingsPanel extends UIPanel 
{
    @property({type: Button})
    private musicBtn: Button;

    @property({type: Button})
    private soundBtn: Button;

    @property({type: Button})
    private closeBtn: Button;

    @property({type: Label})
    private musicLabel: Label;

    @property({type: Label})
    private soundLabel: Label;

    private musicToggle: boolean = true;
    private soundToggle: boolean = true;

    private settingsData: SettingsData;
    private gameSettingsManager: GameSettingsManager;
    
    private hasSavedSettingsData: boolean = false;

    private fn_SetGameSettingsManager()
    {
        this.gameSettingsManager = this.uiManager.MainManager.fn_GetMainManager<GameSettingsManager>(ManagerType.GAME_SETTINGS);
    }

    protected onEnable(): void 
    {
        super.onEnable();    

        this.fn_SetSoundSettingsData();

        this.musicBtn.node.on(Button.EventType.CLICK, () => this.fn_OnMusicToggle());
        this.soundBtn.node.on(Button.EventType.CLICK, () => this.fn_OnSoundToggle());
        this.closeBtn.node.on(Button.EventType.CLICK, () => 
        {
            this.uiManager.fn_DeactivateCurrentActivePanel()
            this.uiManager.fn_ActivatePanel(PopupType.PT_MainMenu);
        });
    }

    public fn_InitSettingsData(data: SettingsData)
    {
        this.settingsData = data;
    }

    public fn_SetSoundSettingsData() 
    {
        if (this.settingsData) 
        {
            this.fn_SetGameSettingsManager();

            this.fn_SetMusicToggle(this.settingsData.audioSettingsData.musicToggle);
            this.fn_SetSoundToggle(this.settingsData.audioSettingsData.soundToggle);

            this.fn_SetMusicSprite();
            this.fn_SetSoundSprite();
        }
    }

    protected onDisable(): void 
    {
        this.fn_SaveSettingsData();

        this.musicBtn.node.off(Button.EventType.CLICK);
        this.soundBtn.node.off(Button.EventType.CLICK);
        this.closeBtn.node.off(Button.EventType.CLICK);
    }

    private fn_SaveSettingsData()
    {
        // this.uiManager.fn_PassSettingsInfo(this.settingsData);
        this.uiManager.fn_SaveNewData<GameSettingsManager>(this.settingsData, ManagerType.GAME_SETTINGS);
    }

    private fn_SetMusicToggle(state: boolean)
    {
        this.musicToggle = state;
        this.settingsData.audioSettingsData.musicToggle = this.musicToggle;
        // initialize the sound toggle state as well

        this.gameSettingsManager.fn_SendUpdateToSoundManager(this.settingsData.audioSettingsData);
        this.fn_SaveSettingsData();
    }

    private fn_SetSoundToggle(state: boolean)
    {
        this.soundToggle = state;
        // initialize the music toggle state as well
        this.settingsData.audioSettingsData.soundToggle = this.soundToggle;

        this.gameSettingsManager.fn_SendUpdateToSoundManager(this.settingsData.audioSettingsData);
        this.fn_SaveSettingsData();
    }

    private fn_OnMusicToggle()
    {
        console.log("fn_OnMusicToggle: " + this.musicToggle);
        this.fn_SetMusicToggle(!this.musicToggle);
        
        this.fn_SetMusicSprite();
    }

    private fn_OnSoundToggle()
    {
        console.log("fn_OnSoundToggle: " + this.soundToggle);
        this.fn_SetSoundToggle(!this.soundToggle);
        
        this.fn_SetSoundSprite();
    }

    private fn_SetMusicSprite()
    {
        console.log("fn_SetMusicSprite: " + this.musicToggle);
        
        this.musicLabel.string = this.musicToggle ? "ON" : "OFF";
    }
    
    private fn_SetSoundSprite()
    {
        console.log("fn_SetSoundSprite: " + this.soundToggle);
        this.soundLabel.string = this.soundToggle ? "ON" : "OFF";
    }
}

