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

namespace Darkmatter.Features.DrawingCatalog
{
    public class DrawingCatalogView : MonoBehaviour
    {
        [SerializeField] private RectTransform content;
        [SerializeField] private DrawingCatalogButton buttonPrefab;
        private readonly List<DrawingCatalogButton> _buttons = new();

        public event Action<string> OnItemClicked;

        public void SetItems(IReadOnlyList<CatalogItemVM> items)
        {
            while (_buttons.Count < items.Count)
            {
                var button = Instantiate(buttonPrefab, content);
                var item = items[_buttons.Count];
                button.Initialize(item.Id, item.Thumbnail,
                    () => { OnItemClicked?.Invoke(item.Id); });
                _buttons.Add(button);
            }

            while (_buttons.Count > items.Count)
            {
                var last = _buttons[^1];
                _buttons.RemoveAt(_buttons.Count - 1);
                Destroy(last.gameObject);
            }
        }
    }
}