using System;
using System.Collections.Generic;
using System.Linq;
using Intrepid.Utilities;
using Sirenix.OdinInspector;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

namespace Intrepid.Gameplay.Codex.Visual
{
    /// <summary>
    /// Catalog of all pictorial protocols available in the game.
    /// Currently uses the existing Theme asset as the concrete protocol asset.
    /// </summary>
    [Serializable]
    [CreateAssetMenu(fileName = "Codex - Pictorial Protocol Library", menuName = "GON/CODEX/Visual/Pictorial Protocol Library")]
    public class PictorialProtocolLibrary : SingletonScriptableObject<PictorialProtocolLibrary>
    {
        [GroupInjections, SerializeField, ReadOnly] private List<PictorialProtocolTag> protocols;

        public IReadOnlyList<PictorialProtocolTag> Protocols => protocols.Where(p => p.IsNotNull()).Distinct().ToList();

        public bool Contains(PictorialProtocolTag protocol)
        {
            return protocol is not null && protocols.Contains(protocol);
        }

#if UNITY_EDITOR
        [PropertyOrder(-10)]
        [GroupInjections, Button(ButtonSizes.Large), GUIColor(0.35f, 0.8f, .35f)]
        protected void FindAll()
        {
            protocols.Clear();
            var guids = AssetDatabase.FindAssets($"t:{nameof(PictorialProtocolTag)}");
            foreach (var guid in guids)
            {
                var path = AssetDatabase.GUIDToAssetPath(guid);
                var protocol = AssetDatabase.LoadAssetAtPath<PictorialProtocolTag>(path);
                protocols.Add(protocol);
            }
            EditorUtility.SetDirty(this);
        }
#endif
    }
}
