import { _decorator, ccenum, Component, Node } from 'cc';
import { PopupType } from '../../../UI/popup/PopupType';
import { UIPanel } from '../../../UI/base/UIPanel';
const { ccclass, property } = _decorator;

@ccclass('PopupManager')
export class PopupManager extends Component 
{
    @property({type: [UIPanel]})
    private m_Panels: UIPanel[] = [];

    private m_PanelsMap: Map<PopupType, UIPanel>  = new Map<PopupType, UIPanel>();
    private m_ActivePanel: UIPanel;

    private static m_Instance: PopupManager;
    
    public static get m_GetInstance() : PopupManager
    {
        return PopupManager.m_Instance;
    }
    
    public get fn_GetActivePanel() : UIPanel
    {
        return this.m_ActivePanel;
    }

    public get fn_PanelsCount() : number
    {
        return this.m_PanelsMap.size;
    }

    protected onLoad(): void 
    {    
        this.fn_InitMap();
        if (!PopupManager.m_Instance)
        {
            PopupManager.m_Instance = this;
            return;
        }
        
        this.destroy();
    }

    public fn_InitMap()
    {
        this.m_PanelsMap = new Map<PopupType, UIPanel>();
        this.m_Panels.forEach((panel) => 
        {
            this.m_PanelsMap.set(panel.fn_GetPopupType, panel)
        });
    }

    public fn_ActivatePanel(popupType: PopupType)
    {
        let page = this.fn_GetPanelFromMap(popupType);
        page.fn_ShowPanel();
        this.m_ActivePanel = page;
    }

    public fn_DeactivateAllPanels()
    {
        this.m_PanelsMap.forEach(panel => () => panel.fn_HidePanel());
    }

    public fn_DeactivatePanel()
    {
        if (this.m_ActivePanel)
        {
            this.m_ActivePanel.fn_HidePanel();
            this.m_ActivePanel = null;
        }
    }

    public fn_DeactivateSpecificPanel(popupType: PopupType)
    {
        let page = this.fn_GetPanelFromMap(popupType);
        page.fn_HidePanel();
    }

    public fn_GetPanelFromMap(popupType: PopupType) : UIPanel
    {
        let panel = this.m_PanelsMap.get(popupType);
        return panel;
    }

    public fn_IteratePanelFromMap(idx: number) : UIPanel
    {
        return this.m_Panels[idx];
    }
}

