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

namespace BingoBrain.Core
{
    public class ShungTik
    {
        private Transform m_releaseRoot;
        private Transform m_getRoot;
        private Action<GameObject> m_onNew;
        private Action<GameObject> m_onGet;
        private Action<GameObject> m_onRelease;

        private Dictionary<string, Shungbass> m_pools = new();

        public int CountAll
        {
            get
            {
                int countAll = 0;
                foreach (Shungbass poolItem in m_pools.Values)
                {
                    countAll += poolItem.CountAll;
                }

                return countAll;
            }
        }

        public int CountInactive
        {
            get
            {
                int countInactive = 0;
                foreach (Shungbass poolItem in m_pools.Values)
                {
                    countInactive += poolItem.CountInactive;
                }

                return countInactive;
            }
        }

        public int CountActive
        {
            get
            {
                int countActive = 0;
                foreach (Shungbass poolItem in m_pools.Values)
                {
                    countActive += poolItem.CountActive;
                }

                return countActive;
            }
        }

        public void InitRoot(Transform releaseRoot = null, Transform getRoot = null)
        {
            m_releaseRoot = releaseRoot;
            m_getRoot = getRoot;
        }

        public void InitCallBack(Action<GameObject> onNew, Action<GameObject> onGet, Action<GameObject> onRelease)
        {
            m_onNew = onNew;
            m_onGet = onGet;
            m_onRelease = onRelease;
        }




        public GameObject Get(string prefabPath, string assetName)
        {
            var poolName = $"{prefabPath} | {assetName}";
            if (!m_pools.ContainsKey(poolName))
            {
                RegisterNew(prefabPath, assetName);
            }

            Shungbass pool = m_pools[poolName];
            return pool.Get();
        }




        public void Release(string prefabPath, GameObject obj)
        {
            Shungbass pool = null;
            if (m_pools.TryGetValue(prefabPath, out pool))
            {
                pool.Release(obj);
            }
        }




        public void Release(GameObject obj)
        {
            Shungbass pool = null;
            foreach (Shungbass poolItem in m_pools.Values)
            {
                if (poolItem.Contains(obj))
                {
                    pool = poolItem;
                    break;
                }
            }

            pool.Release(obj);
        }




        public void ReleaseAll()
        {
            foreach (Shungbass poolItem in m_pools.Values)
            {
                poolItem.ReleaseAll();
            }
        }




        private void RegisterNew(string prefabPath, string assetName)
        {
            Shungbass pool = new Shungbass();
            pool.Init(prefabPath, assetName, m_releaseRoot, m_getRoot);
            pool.InitCallBack(m_onNew, m_onGet, m_onRelease);
            m_pools.Add($"{prefabPath} | {assetName}", pool);
        }




        public void ClearPool(string prefabPath)
        {
            Shungbass pool = null;
            if (m_pools.TryGetValue(prefabPath, out pool))
            {
                pool.Clear();
                m_pools.Remove(prefabPath);
            }
        }




        public void Clear()
        {
            foreach (Shungbass pool in m_pools.Values)
            {
                pool.Clear();
            }

            m_pools.Clear();
        }




        public void Dispose()
        {
            Clear();
            m_pools = null;
        }
    }
}