using System.Collections.Generic;
using System.Threading;



namespace CombatLibrary.CombatLibrary.CombatCore
{
    public class CombatPool<T> where T : new()
    {
        private List<T> allPool = new List<T>();
        public int count = 1000;
        private HashSet<int> _map = new HashSet<int>();
        public T Get()
        {
            // lock (allPool)
            {
                if (allPool.Count > 0)
                {
                    T betterList = allPool[0];
                    allPool.RemoveAt(0);
                    // System.GC.ReRegisterForFinalize(betterList);
                    int code = betterList.GetHashCode();
                    _map.Remove(code);
                    
                    return betterList;
                }
                else
                {
                    T betterList = new T();

                    return betterList;
                }
            }
        }

        public void Clear()
        {
            allPool.Clear();
            _map.Clear();
        }

        public void Recycle(T a)
        {
            if (a != null)
            {
                // lock (allPool)
                {
                    int code = a.GetHashCode();
                    if (_map.Count > 1000)
                    {
                        return;
                    }

                    if (!_map.Contains(code))
                    {
                        // System.GC.SuppressFinalize(a);
                        allPool.Add(a);
                        _map.Add(code);
                    }
                }
            }
        }
    }
}