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


namespace Rokid.MRC
{
    public class UIManager : UnitySingleton<UIManager>
    {
        public bool HideUISystem;

        public List<GameObject> UIPrefabList;

        private Dictionary<UIType, UIPanelBase> UIPanelDic = new Dictionary<UIType, UIPanelBase>();

        private List<UIType> OpendUIPanels = new List<UIType>();

        public void Init()
        {
        }

        public UIPanelBase CreatePanel(UIType type)
        {
            UIData uiData = GetUIDataByType(type);
            if(uiData != null)
            {
                return CreatePanel(uiData);
            }

            return null;
        }

        private UIPanelBase CreatePanel(UIData uiData)
        {
            if(GetLoadedPanel(uiData.uiType) != null)
            {
                Debug.LogError($"CreatePanel {uiData.uiType} Already Created");
            }

            if(uiData != null)
            {
                //todo，目前直接从子物体下找
                GameObject uiObj = transform.Find(uiData.uiPrefabPath)?.gameObject;

                if(uiObj == null)
                {
                    uiObj = GetUIPrefab(uiData);
                }

                if(uiObj == null)
                {
                    Debug.LogError("No Panel In Hierarchy Or In Reference");
                    return null;
                }

                uiObj = Instantiate<GameObject>(uiObj, transform);

                UIPanelBase panel = uiObj.GetComponent<UIPanelBase>();

                panel.GetComponent<Canvas>().sortingOrder = uiData.uiDepth;
                panel.OnInit();

                UIPanelDic.Add(uiData.uiType, panel);

                return panel;
            }

            return null;
        }

        private GameObject GetUIPrefab(UIData uiData)
        {
            foreach(var obj in UIPrefabList)
            {
                if(obj.GetComponent<UIPanelBase>().GetType().Name == uiData.uiPrefabPath)
                {
                    return obj;
                }
            }

            return null;
        }

        public UIPanelBase GetLoadedPanel(UIType type)
        {
            UIPanelDic.TryGetValue(type, out UIPanelBase panel);

            return panel;
        }

        //todo，先简单写一下
        public UIPanelBase OpenPanel(UIType uiType)
        {
            if(HideUISystem)
            {
                return null;
            }

            UIData uIData = GetUIDataByType(uiType);

            //创建界面
            UIPanelDic.TryGetValue(uiType, out UIPanelBase panel);
            if(panel == null)
            {
                panel = CreatePanel(uIData);
            }

            DoRelationShip(uIData);

            return panel;
        }

        private void DoRelationShip(UIData uIData)
        {
            foreach(var panelPair in UIPanelDic)
            {
                if(panelPair.Key == uIData.uiType)
                {
                    panelPair.Value.gameObject.SetActive(true);

                    panelPair.Value.OnOpen();
                }
                else
                {
                    UIData otherData = GetUIDataByType(panelPair.Key);
                    if(otherData != null && !otherData.ignoreOpen)
                    {
                        panelPair.Value.OnClose();
                        panelPair.Value.gameObject.SetActive(false);
                    }
                }
            }
        }

        public void ClosePanel(UIType uiType)
        {
            foreach(var panelPair in UIPanelDic)
            {
                if(panelPair.Key == uiType)
                {
                    panelPair.Value.OnClose();
                    panelPair.Value.gameObject.SetActive(false);
                }
            }
        }

        public void CloseAllPanel()
        {
            foreach(var panelPair in UIPanelDic)
            {
                UIData uIData = GetUIDataByType(panelPair.Key);

                if(uIData != null && !uIData.ignoreOpen)
                {
                    panelPair.Value.OnClose();
                    panelPair.Value.gameObject.SetActive(false);
                }
            }
        }

        public UIData GetUIDataByType(UIType type)
        {
            foreach(UIData uiData in UIDefines.UIDataList)
            {
                if(uiData.uiType == type)
                {
                    return uiData;
                }
            }

            return null;
        }

        public void ShowTips(string desc)
        {
            UITipsPanel tipsPanel = (UITipsPanel)GetLoadedPanel(UIType.Tips);
            tipsPanel?.SetDesc(desc);
            tipsPanel?.gameObject.SetActive(true);
        }

        public void ShowConfirm(string title, string content, ConfirmType confirmType, Action confirmCall = null, Action cancelCall = null)
        {
            UIConfirmPanel confirmPanel = (UIConfirmPanel)OpenPanel(UIType.Confirm);
            confirmPanel?.SetContent(title, content, 3, confirmType, confirmCall, cancelCall);
            confirmPanel?.gameObject.SetActive(true);
        }
    }
}