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.Knowledge
{
    [Serializable]
    [CreateAssetMenu(fileName = "Codex - Concept Library", menuName = "GON/CODEX/Knowledge/Concept Library")]
    public class CodexConceptLibrary : SingletonScriptableObject<CodexConceptLibrary>
    {
        [GroupInjections, SerializeField] private List<CodexConceptTag> concepts = new();

        public IReadOnlyList<CodexConceptTag> Concepts => concepts.Where(c => c.IsNotNull()).Distinct().ToList();

        public bool Contains(CodexConceptTag concept)
        {
            return concept.IsNotNull() && concepts.Contains(concept);
        }

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