using System;
using System.Collections.Generic;
using Assets.Source.Patterns;
using Assets.Source.Patterns.ObjectPool;
using UnityEngine;

namespace Assets.Source.Core.Managers
{
    public class PoolWarmer : Singleton<PoolWarmer>
    {
        [Serializable]
        public class WarmedObject
        {
            public GameObject Prefab;
            public int Count;
        }

        [SerializeField] private List<WarmedObject> _warmedObjects;

        protected override void Init()
        {
            foreach (var warm in _warmedObjects)
            {
                if (warm.Prefab == null)
                    continue;
                var poolPrefab = warm.Prefab.GetComponent<IPoolObject>();
                if (poolPrefab == null)
                    continue;

                PoolManager.Instance.Warm(poolPrefab, warm.Count);
            }
        }

        void OnValidate()
        {
            for (var i = 0; i < _warmedObjects.Count; i++)
            {
                var warmedObject = _warmedObjects[i];
                if (warmedObject.Prefab == null)
                {
                    //Debug.LogError("[PoolWarmer] null warmed object prefab!");
                    continue;
                }
                var poolPrefab = warmedObject.Prefab.GetComponent<IPoolObject>();
                if (poolPrefab == null)
                {
                    //_warmedObjects.RemoveAt(i--);
                    _warmedObjects[i].Prefab = null;
                    Debug.LogError("[PoolWarmer] No IPoolObject on prefab!");
                    continue;
                }
            }
        }
    }
}
