using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager : MonoBehaviour
{
    private GameObject mMainPanel;
    public GameObject MainPanel
    {
        get
        {
            return this.mMainPanel;
        }
        set
        {
            this.mMainPanel = value;
        }
    }

    private UserInfoController mUserInfoPanel;
    public UserInfoController UserInfoPanel
    {
        get
        {
            return this.GetPanelInstance<UserInfoController>("UserInfoPanel", ref this.mUserInfoPanel);
        }
    }

    public static UIManager Instance;

    private void Awake()
    {
        UIManager.Instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private T GetPanelInstance<T>(string prefabName, ref T internalComponent)
    {
        if (internalComponent == null)
        {
            UnityEngine.Object @object = Resources.Load("UI/Prefab/Panel/" + prefabName);
            if (@object != null)
            {
                GameObject gameObject = (GameObject)UnityEngine.Object.Instantiate(@object, base.transform.parent, false);
                if (gameObject != null)
                {
                    internalComponent = gameObject.GetComponent<T>();
                }
            }
        }

        return internalComponent;
    }

    public static void Show(MonoBehaviour panelToShow, GameObject parent = null)
    {
        if (UIManager.Instance == null || panelToShow == null)
        {
            return;
        }
        foreach (Transform child in parent.transform)
        {
            //GameObject.Destroy(child);
            //GameObject.Destroy(child.gameObject);
            child.gameObject.SetActive(false);
        }
        //var panel = UIManager.Instance.MainPanel; //UIManager.Instance.MainCanvasTransform.transform.Find("Panel");

        //panel.gameObject.AddComponent<GameObject>(panelToShow.gameObject);
        panelToShow.transform.SetParent(parent.transform);
        
        panelToShow.gameObject.SetActive(true);        
        panelToShow.transform.position = new Vector3(0, 0, 0);
    }


    //public static void ShowHomePanel()
    //{
    //    UIManager.Show(UIManager.Instance.PanelHome, UIManager.Instance.MainPanel);
    //}

    public static void ShowUserInfoPanel()
    {
        UIManager.Show(UIManager.Instance.UserInfoPanel, UIManager.Instance.MainPanel);
    }

    public static void ShowLoadingDialogBox()
    {
    }


    private void OnDestroy()
    {
        UIManager.Instance = null;
    }
}
