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

namespace TFramework
{
    public interface IPanelData:IReference 
    {
        public void Recycle();
    }

    public class PanelData : IPanelData
    {
        public void Reset()
        {
            
        }

        public void Recycle()=> Main.GetMagr<ReferencePoolManager>().Recycle(this);
    }

    public class BasePanel
    {
        /// <summary>
        /// UI实体
        /// </summary>
        public GameObject UIEntity { get; set; }

        protected Dictionary<string, UIBehaviour> uiComponents;
        protected Dictionary<GameObject, UIBehaviour> ui2gam;

        public virtual bool StickyTopic { get; protected set; } = false;

        public bool IsOpen { get; protected set; }

        /// <summary>
        /// 初始化 UI实体被创建时执行
        /// </summary>
        public virtual void OnInit()
        {
            uiComponents = new Dictionary<string, UIBehaviour>();
            ui2gam = new Dictionary<GameObject, UIBehaviour>();
            GetAllUIBehaviour();
        }

        /// <summary>
        /// 界面显示时
        /// </summary>
        public virtual void OnEnter(IPanelData args = null)
        {
            IsOpen = true;
        }

        /// <summary>
        /// 界面暂停 置于其它界面之下
        /// </summary>
        public virtual void OnPause(IPanelData args = null)
        {

        }

        /// <summary>
        /// 界面继续
        /// </summary>
        public virtual void OnResume(IPanelData args = null)
        {

        }

        /// <summary>
        /// 退出界面 界面关闭
        /// </summary>
        public virtual void OnExit(IPanelData args = null,bool isDestroy=false)
        {
            IsOpen = false;
        }

        /// <summary>
        /// 获取所有UIBehaviour
        /// </summary>
        private void GetAllUIBehaviour()
        {
            foreach (var item in UIEntity.GetComponentsInChildren<UIBehaviour>(true))
            {
                if (uiComponents.ContainsKey(item.ID))
                    throw new UnityException($"在同一个Panel下发现相同的UIBehaviourID：{item.ID}");
                else
                {
                    uiComponents.Add(item.ID, item);
                    ui2gam.Add(item.gameObject, item);
                }
            }
        }

        /// <summary>
        /// 获取UIBehaviour
        /// </summary>
        /// <param name="uiId"></param>
        /// <returns></returns>
        protected UIBehaviour GetUIBehaviour(string uiId)
        {
            if (uiComponents.ContainsKey(uiId))
                return uiComponents[uiId];
            else
            {
                Log.Error($"{UIEntity.name}下未找到ID为{uiId}的UIBehaviour！");
                return null;
            }
        }

        protected T GetComponentInUIID<T>(string uiId) where T: Component
        {
            return GetUIBehaviour(uiId)?.GetComponent<T>();
        }

        protected Component  GetComponentInUIID(Type type, string uiId)
        {
            return GetUIBehaviour(uiId)?.GetComponent(type);
        }

        protected T GetUseRes<T>(GameObject ui,string resName) where T : UnityEngine.Object
        {
            if (ui2gam.ContainsKey(ui))
                return ui2gam[ui].GetRes(resName) as T;

            Debug.Log($"对象{ui.name}的资源列表中没有名字为{resName}的资源");
            return null;
        }

        /// <summary>
        /// 切换语言
        /// </summary>
        internal void ChangedWord()
        {
            foreach (var item in uiComponents.Values)
            {
                Text text = item.GetComponent<Text>();
                if (!text) continue;
                string str = Main.GetMagr<LanguageManager>()?.GetLanguageText(text.text);
                if (string.IsNullOrEmpty(str)) continue;
                text.text = str;
            }
        }
    }
}

