using System.Collections.Generic;
using UnityEngine;

namespace YachtDice.Categories
{
    /// <summary>
    /// Каталог всех доступных категорий.
    /// Порядок определяет порядок отображения в UI.
    /// Аналог ModifierCatalogSO.
    /// </summary>
    [CreateAssetMenu(fileName = "CategoryCatalog", menuName = "YachtDice/Categories/Catalog")]
    public class CategoryCatalog : ScriptableObject
    {
        [SerializeField] private List<CategoryDefinition> categories = new();

        public IReadOnlyList<CategoryDefinition> All => categories;
        public int Count => categories.Count;

        public CategoryDefinition FindById(string id)
        {
            for (int i = 0; i < categories.Count; i++)
            {
                if (categories[i] != null && categories[i].Id == id)
                    return categories[i];
            }
            return null;
        }

        public int IndexOf(CategoryDefinition def)
        {
            for (int i = 0; i < categories.Count; i++)
            {
                if (categories[i] == def)
                    return i;
            }
            return -1;
        }

#if UNITY_EDITOR
        public static CategoryCatalog CreateForTest(List<CategoryDefinition> defs)
        {
            var catalog = CreateInstance<CategoryCatalog>();
            catalog.categories = defs ?? new List<CategoryDefinition>();
            return catalog;
        }
#endif
    }
}
