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

namespace TFramework
{
    public class ObjectPoolMananger : BaseManager
    {
        public Transform m_poolTransform;

        private Dictionary<string, ObjectPool> _poolDic = new Dictionary<string, ObjectPool>();

        /// <summary>
        /// 注册对象池
        /// </summary>
        /// <param name="poolName">对象池名字</param>
        /// <param name="objTemplate">对象模板</param>
        /// <param name="maxCount">池子最大量</param>
        /// <param name="takeOutEvent">取出事件</param>
        /// <param name="recycleEvent">回收事件</param>
        public void RegisterPool(string poolName, GameObject objTemplate, int maxCount, TAction<GameObject> takeOutEvent = null, TAction<GameObject> recycleEvent = null)
        {
            if (_poolDic.ContainsKey(poolName))
                Log.Error($"对象池【{poolName}】已注册，请勿重复注册！");
            else
            {
                new GameObject(poolName).transform.parent = m_poolTransform;
                _poolDic.Add(poolName, new ObjectPool(objTemplate, maxCount, takeOutEvent, recycleEvent));
            }
        }

        /// <summary>
        /// 取出对象
        /// </summary>
        /// <param name="poolName"></param>
        /// <returns></returns>
        public GameObject TakeOutObj(string poolName)
        {
            if (_poolDic.ContainsKey(poolName))
                return _poolDic[poolName].TakeOutObj();
            else
            {
                Log.Error($"不存在名字为【{poolName}】的对象池！");
                return null;
            }
        }

        /// <summary>
        /// 回收对象
        /// </summary>
        /// <param name="poolName">对象池名字</param>
        /// <param name="obj">对象</param>
        public void RecycleObj(string poolName, GameObject obj)
        {
            if (_poolDic.ContainsKey(poolName))
            {
                obj.transform.SetParent(m_poolTransform.Find(poolName),false);
                _poolDic[poolName].RecycleObj(obj);
            }
            else
                Log.Error($"不存在名字为【{poolName}】的对象池！");
        }

        /// <summary>
        /// 批量回收对象
        /// </summary>
        /// <param name="poolName"></param>
        /// <param name="objs"></param>
        public void RecycleObjs(string poolName, IEnumerable<GameObject> objs)
        {
            foreach (var item in objs)
            {
                RecycleObj(poolName, item);
            }
        }

        /// <summary>
        /// 检查是否存在对象池
        /// </summary>
        /// <param name="poolName"></param>
        /// <returns></returns>
        public bool IsHaveObjPool(string poolName) => _poolDic.ContainsKey(poolName);

        /// <summary>
        /// 清空指定对象池
        /// </summary>
        public void ClearObjpool(string poolName)
        {
            if (IsHaveObjPool(poolName))
                _poolDic[poolName].Clear();
        }

        /// <summary>
        /// 清空所有对象池
        /// </summary>
        public void ClearAll()
        {
            foreach (var item in _poolDic)
                item.Value.Clear();
        }
    }
}
