using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

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

    private HomeController mHomePanel;
    public HomeController HomePanel
    {
        get
        {
            return this.GetPanelInstance("HomePanel", ref this.mHomePanel);
        }
    }

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

    private DeviceController mDeviceController;
    public DeviceController DevicePanel
    {
        get
        {
            return this.GetPanelInstance("DevicePanel", ref this.mDeviceController);
        }
    }
    
    private MapListController mMapListController;
    public MapListController MapListPanel
    {
        get
        {
            return this.GetPanelInstance("MapListPanel", ref this.mMapListController);
        }
    }

    private EditUserController mEditUserController;
    public EditUserController EditUserPanel
    {
        get
        {
            return this.GetPanelInstance("EditUserPanel", ref this.mEditUserController);
        }
    }

    private GameObject mModalsPanel;
    public GameObject ModalsPanel
    {
        get
        {
            return this.mModalsPanel;
        }
        set
        {
            this.mModalsPanel = value;
        }
    }


    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, bool modal = false)
    {
        if (UIManager.Instance == null || panelToShow == null)
        {
            return;
        }

        if (modal)
        {
            parent = UIManager.Instance.ModalsPanel;
        }

        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);
        panelToShow.transform.localPosition = new Vector3(0, 0, 0);
        //panelToShow.transform.localRotation = new Quaternion(0, 0, 0, 0);
        panelToShow.transform.GetComponent<RectTransform>().offsetMin = new Vector2(0, 0);
        panelToShow.transform.GetComponent<RectTransform>().offsetMax = new Vector2(0, 0);
        panelToShow.transform.localScale = parent.transform.localScale;

    }


    //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 ShowHomePanel()
    {
        UIManager.Show(UIManager.Instance.HomePanel, UIManager.Instance.MainPanel);
    }

    public static void ShowDevicePanel()
    {
        UIManager.Show(UIManager.Instance.DevicePanel, UIManager.Instance.MainPanel);
    }

    public static void ShowEditUserPanel()
    {
        UIManager.Show(UIManager.Instance.EditUserPanel, UIManager.Instance.MainPanel);
    }

    public static void ShowLoadingDialogBox()
    {
    }
    public static void ShowMapListPanel() 
    {
        UIManager.Show(UIManager.Instance.MapListPanel, UIManager.Instance.MainPanel);
    }
    public static void AddEvent(GameObject gameObject, EventTriggerType eventTriggerType, UnityAction<BaseEventData> call)
    {
        EventTrigger et = gameObject.GetComponent<EventTrigger>();
        if (et == null)
        {
            et = gameObject.AddComponent<EventTrigger>();
        }
        EventTrigger.Entry pointerEvent = new EventTrigger.Entry();
        pointerEvent.eventID = eventTriggerType;
        pointerEvent.callback.AddListener(call);
        et.triggers.Add(pointerEvent);
    }


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