using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using UnityEngine.Pool;

public class GameObjectPool {
  private ObjectPool<GameObject> Pool;

  public GameObjectPool(GameObject prefab, int defaultSize = 10, int maxSize = 50) {
    var container = new GameObject(prefab.name + " Pool");
    Pool = new ObjectPool<GameObject>(
        createFunc: () => {
          var obj = Object.Instantiate(prefab, container.transform);
          obj.SetActive(false);
          return obj;
        },
        actionOnGet: obj => { obj.SetActive(true); },
        actionOnRelease: obj => { obj.SetActive(false); },
        actionOnDestroy: obj => Object.Destroy(obj),
        collectionCheck: true,
        defaultCapacity: defaultSize,
        maxSize: maxSize
    );
  }

  public GameObject Get(Vector3? position = null, Quaternion? rotation = null) {
    if (position == null) position = Vector3.zero;
    if (rotation == null) rotation = Quaternion.Euler(0, 0, 0);

    var obj = Pool.Get();
    obj.transform.SetPositionAndRotation((Vector3)position, (Quaternion)rotation);
    return obj;
  }

  public void Release(GameObject obj) => Pool.Release(obj);
}
