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

namespace ChillConnect
{
    public class GObjectsPool
    {
        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, GObjectPool> m_pools = new();

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

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

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