// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import GUIProxy from "./GUIProxy";


export default class GUIManager{

    /**
     * UI资源包
     */
    AssetBundle:cc.AssetManager.Bundle;

    //UI 注册信息
    private m_guiMap:Map<number,GUIProxy>=new Map<number,GUIProxy>();
    //正在显示的UI
    private m_showing:Map<number,GUIProxy>=new Map<number,GUIProxy>();

    /**
     * 注册UI
     * @param key           UI 全局唯一KEY
     * @param Prefab    预制体地址
     */
    RegisterGUI(key:number,Prefab:string):void{
        if(this.m_guiMap.has(key)){
            throw new Error("UI重复注册:"+key);
        }
        let guiProxy:GUIProxy=new GUIProxy(key,Prefab);
        this.m_guiMap.set(key,guiProxy);
    }

    /**
     * 取消注册
     * @param key           UI 全局唯一KEY
     */
    Unregister(key:number):void{
        if(this.m_guiMap.has(key)==false){
            throw new Error("无法取消未注册的UI："+key);
        }
    }

    /**
     * 显示UI
     * @param key 
     * @param data 
     */
    Show(key:number,data?:any):void{
        if(this.m_guiMap.has(key)==false){
            throw new Error("未注册的UI："+key);
        }
        let guiProxy:GUIProxy;
        //已经显示
        if(this.m_showing.has(key)){
            return;
        }else{
            guiProxy=this.m_guiMap.get(key);
            guiProxy.Show();
            this.m_showing.set(key,guiProxy);
        }
    }

    /**
     * 隐藏UI
     * @param key 
     */
    Hide(key:number):void{
        if(this.m_guiMap.has(key)==false){
            throw new Error("未注册的UI："+key);
        }
        let guiProxy:GUIProxy;
        //已经显示
        if(this.m_showing.has(key)==false){
            return;
        }else{
            guiProxy=this.m_guiMap.get(key);
            guiProxy.Hide();
        }
    }

    /**
     * 单例
     */
    private static instance:GUIManager;
    public static get single():GUIManager{
        if(this.instance==null){
            this.instance=new GUIManager();
        }
        return this.instance;
    }
}