using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
public abstract class SimpleMultiPool<T> : UniRx.Toolkit.ObjectPool<T> where T : UnityEngine.Component
{

    protected List<T> Prefabs { get; set; } = new List<T>();
    protected string PoolObjectName { get; set; }
    public SimpleMultiPool(IEnumerable<T> prefab, string poolObjectName = "pool")
    {
        this.Prefabs.AddRange(prefab.AsEnumerable());
        PoolObjectName = poolObjectName;
    }
    protected override void OnBeforeReturn(T instance)
    {
        base.OnBeforeReturn(instance);
        instance.transform.parent = GetParentObject.transform;

    }
    protected override void OnBeforeRent(T instance)
    {
        base.OnBeforeRent(instance);
        instance.transform.parent = null;
    }
    GameObject GetParentObject
    {
        get
        {
            var pool = GameObject.Find(PoolObjectName);
            if (pool == null)
            {
                pool = new GameObject(PoolObjectName);
            }
            return pool;
        }
    }
    System.Random random = new System.Random(634534);

    protected override T CreateInstance()
    {
        var z = random.Next(0, Prefabs.Count - 1);
        return GameObject.Instantiate(Prefabs[z]);
    }

}
