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

public class UIManager : S_MonoSingleton<UIManager>
{
    public bool IsUICovered
    {
        get
        {
            return mUIList.Count > 0;
        }
    }

    public Canvas UICanvas
    {
        get
        {
            if (mUICanvas == null)
            {
                mUICanvas = GetComponent<Canvas>();
            }

            return mUICanvas;
        }
    }

    public Camera UICamera
    {
        get
        {
            if (mUICamera == null)
            {
                mUICamera = UICanvas.worldCamera;
            }

            return mUICamera;
        }
    }

    private Canvas mUICanvas;
    private Camera mUICamera;
    private Dictionary<Type, BasePanel> mUIDic = new Dictionary<Type, BasePanel>();
    private List<BasePanel> mUIList = new List<BasePanel>();

    public T OpenUI<T>() where T : BasePanel
    {
        T tUI = GetUI<T>();
        tUI.gameObject.SetActive(true);
        tUI.OnOpen();

        if (mUIList.Contains(tUI))
        {
            mUIList.Remove(tUI);
        }
        if (mUIList.Count > 0)
        {
            mUIList[mUIList.Count - 1].OnFocus(false);
        }
        mUIList.Add(tUI);

        return tUI;
    }

    public T CloseUI<T>() where T : BasePanel
    {
        T tUI = GetUI<T>();
        CloseUI(tUI);

        return tUI;
    }

    public void CloseUI(BasePanel pBasePanel)
    {
        pBasePanel.gameObject.SetActive(false);
        pBasePanel.OnClose();

        int tIndex = mUIList.IndexOf(pBasePanel);
        if (tIndex == mUIList.Count - 1)
        {
            mUIList.Remove(pBasePanel);
            if (mUIList.Count > 0)
            {
                mUIList[mUIList.Count - 1].OnFocus(true);
            }
        }
        else
        {
            mUIList.Remove(pBasePanel);
        }
    }

    public T GetUI<T>() where T : BasePanel
    {
        T tUI = null;
        if (!mUIDic.ContainsKey(typeof(T)))
        {
            mUIDic[typeof(T)] = GetComponentInChildren<T>(true);
        }

        tUI = mUIDic[typeof(T)] as T;

        return tUI;
    }

    public void ShowTip(string pTip)
    {

    }

    public static bool IsTouchUI()
    {
        if (EventSystem.current != null)
        {
            PointerEventData eventData = new PointerEventData(EventSystem.current);
            eventData.position = Input.mousePosition;
            List<RaycastResult> results = new List<RaycastResult>();
            EventSystem.current.RaycastAll(eventData, results);
            if (results == null || results.Count == 0)
            {
                return false;
            }
            return results.Count > 0 && results[0].gameObject.layer == 5;
        }
        return false;
    }

    public static List<GameObject> GetChildGobList(Transform pParent)
    {
        List<GameObject> tList = new List<GameObject>();

        for (int i = 0; i < pParent.childCount; i++)
        {
            tList.Add(pParent.GetChild(i).gameObject);
        }

        return tList;
    }

    public static List<T> GetChildList<T>(Transform pParent)
    {
        List<T> tList = new List<T>();

        for (int i = 0; i < pParent.childCount; i++)
        {
            tList.Add(pParent.GetChild(i).GetComponent<T>());
        }

        return tList;
    }
}