using Assets.Source.Patterns.ObjectPool;
using Assets.Source.Utils;
using UnityEngine;

namespace Assets.Source.General
{
    public static class Spawner
    {
        public static T Spawn<T>(T prefab, Vector3 pos, Quaternion rotation) where T : Object, IPoolObject
        {
            if (PoolManager.Instance != null && PoolManager.AwakeDone)
                return PoolManager.Instance.Spawn(prefab, pos, rotation);

            return Instantiator.Instantiate(prefab, pos, rotation);
        }

        public static T Spawn<T>(T prefab, Vector3 pos, Vector3 rotation) where T : Object, IPoolObject
        {
            return Spawn(prefab, pos, Quaternion.Euler(rotation));
        }

        public static T Spawn<T>(T prefab, Vector3 pos) where T : Object, IPoolObject
        {
            return Spawn(prefab, pos, Quaternion.identity);
        }

        public static T Spawn<T>(T prefab) where T : Object, IPoolObject
        {
            return Spawn(prefab, Vector3.zero, Quaternion.identity);
        }

        public static IPoolObject Spawn(IPoolObject prefab, Vector3 pos, Quaternion rotation)
        {
            if (PoolManager.Instance != null && PoolManager.AwakeDone)
                return PoolManager.Instance.Spawn(prefab, pos, rotation);

            var poolObj = Instantiator.InstantiateGO(prefab.gameObject).GetComponent<IPoolObject>();
            poolObj.transform.position = pos;
            poolObj.transform.rotation = rotation;

            return poolObj;
        }

        public static IPoolObject Spawn(IPoolObject prefab, Vector3 pos)
        {
            return Spawn(prefab, pos, Quaternion.identity);
        }

        public static IPoolObject Spawn(IPoolObject prefab)
        {
            return Spawn(prefab, Vector3.zero, Quaternion.identity);
        }

        public static void Release(IPoolObject obj)
        {
            if (PoolManager.Instance != null && PoolManager.AwakeDone)
                PoolManager.Instance.Release(obj);
            if (!Application.isPlaying)
                Object.DestroyImmediate(obj.gameObject);
        }
    }
}
