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

public enum E_Layer 
{
    bot,
    lower_mid,
    mid,
    upper_mid,
    lower_top,
    top,
    upper_top,
    system
}

public class UIMgr : BaseMgr<UIMgr>
{
    public bool IsInit=false;

    private Dictionary<string, BasePanel> paneldic = new Dictionary<string, BasePanel>();

    public Camera MainCamera;

    public RectTransform canvas;

    public Transform bot;

    public Transform lower_mid;
    public Transform mid;
    public Transform upper_mid;

    public Transform lower_top;
    public Transform top;
    public Transform upper_top;
    public Transform system;

    public const string PANEL_PATH = "UI/Panel/";
    

    public void ShowPanelAsync<T>(E_Layer layer,UnityAction<T> callback=null, object data = null) where T:BasePanel
    {
        CheckInit();

        string name = typeof(T).ToString();
        if (paneldic.ContainsKey(name))
        {
            callback?.Invoke(paneldic[name] as T);
            return;
        }

        ResMgr.Getinstance().LoadAsync<GameObject>(PANEL_PATH + name, (obj) =>
            {
            
            Transform father=null;
            switch (layer)
            {
                case E_Layer.bot:
                    father = bot;
                    break;
                case E_Layer.lower_mid:
                    father = lower_mid;
                    break;
                case E_Layer.mid:
                    father = mid;
                    break;
                case E_Layer.upper_mid:
                    father = upper_mid;
                    break;
                case E_Layer.lower_top:
                    father = lower_top;
                    break;
                case E_Layer.top:
                    father = top;
                    break;
                case E_Layer.upper_top:
                    father = upper_top;
                    break;
                case E_Layer.system:
                    father = system;
                    break;
            }

            obj.transform.SetParent(father);

            obj.transform.localPosition = Vector3.zero;
            obj.transform.localScale = Vector3.one;

            (obj.transform as RectTransform).offsetMax = Vector2.zero;
            (obj.transform as RectTransform).offsetMin = Vector2.zero;

            
            T panel = obj.transform.GetComponent<T>() ?? obj.AddComponent<T>();

            paneldic.Add(name, panel);

            callback?.Invoke(panel);

            panel.OnInit(data);
        });
    }

    public void ShowPanel<T>(E_Layer layer,UnityAction<T> callback=null,object data = null) where T:BasePanel
    {
        CheckInit();

        string name = typeof(T).ToString();
        if (paneldic.ContainsKey(name))
        {
            callback?.Invoke(paneldic[name] as T);
            return;
        }

        Transform father = null;
        GameObject obj = ResMgr.Getinstance().Load<GameObject>(PANEL_PATH + name);
        switch (layer)
        {
            case E_Layer.bot:
                father = bot;
                break;
            case E_Layer.lower_mid:
                father = lower_mid;
                break;
            case E_Layer.mid:
                father = mid;
                break;
            case E_Layer.upper_mid:
                father = upper_mid;
                break;
            case E_Layer.lower_top:
                father = lower_top;
                break;
            case E_Layer.top:
                father = top;
                break;
            case E_Layer.upper_top:
                father = upper_top;
                break;
            case E_Layer.system:
                father = system;
                break;
        }
        obj.transform.SetParent(father);

        obj.transform.localPosition = Vector3.zero;
        obj.transform.localScale = Vector3.one;

        (obj.transform as RectTransform).offsetMax = Vector2.zero;
        (obj.transform as RectTransform).offsetMin = Vector2.zero;

        T panel = obj.transform.GetComponent<T>() ?? obj.AddComponent<T>();

        paneldic.Add(name, panel);

        callback?.Invoke(panel);

        panel.OnInit(data);
    }

    public void HidePanel<T>(string name = null) where T:BasePanel
    {
        CheckInit();

        string panelname = name ?? typeof(T).ToString();

        if (paneldic.ContainsKey(panelname))
        {
            paneldic[panelname].OnHide();
            GameObject.Destroy(paneldic[panelname].gameObject);
            paneldic.Remove(panelname);
        }
    }

    public T GetPanel<T>() where T:BasePanel
    {
        CheckInit();

        string name = typeof(T).ToString();

        if (paneldic.ContainsKey(name))
        {
            return paneldic[name] as T;
        }
        Debug.LogError("没有找到" + name + "面板");
        return null;
    }

    public void AddCustomEventListener(Transform control,EventTriggerType type,UnityAction<BaseEventData> callback) 
    {
        EventTrigger trigger = control.GetComponent<EventTrigger>() ?? control.gameObject.AddComponent<EventTrigger>();

        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = type;

        entry.callback.AddListener(callback);

        trigger.triggers.Add(entry);
    }

    public void CheckInit() 
    {
        if (IsInit==false)
        {
            GameObject obj = ResMgr.Getinstance().Load<GameObject>("UI/Canvas");
            MainCamera = Camera.main;
            GameObject.DontDestroyOnLoad(MainCamera);
            obj.GetComponent<Canvas>().worldCamera = MainCamera;
            canvas = obj.transform as RectTransform;
            GameObject.DontDestroyOnLoad(obj);

            bot = canvas.Find("bot");
            lower_mid = canvas.Find("lower_mid");
            mid = canvas.Find("mid");
            upper_mid = canvas.Find("upper_mid");
            lower_top = canvas.Find("lower_top");
            top = canvas.Find("top");
            upper_top = canvas.Find("upper_top");
            system = canvas.Find("system");

            obj = ResMgr.Getinstance().Load<GameObject>("UI/EventSystem");
            GameObject.DontDestroyOnLoad(obj);
            IsInit = true;
        }
        
    }
}
