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

namespace Assets.Source.Content.UI.Components
{
    public class PoolItemsGroup<T> where T : IPoolObject
    {
        public Transform Root { get; }
        private Func<List<T>> _spawnFunc;
        private List<T> _items = new List<T>();

        public PoolItemsGroup(Transform root, Func<List<T>> spawnFunc)
        {
            _spawnFunc = spawnFunc;
            Root = root;
        }

        public void Refresh()
        {
            foreach (var item in _items)
                item.Release();
            _items.Clear();

            _items = _spawnFunc?.Invoke();

            foreach (var item in _items)
            {
                item.transform.SetParent(Root, false);
                item.transform.localScale = Vector3.one;
            }
        }
    }
}
