using System;
using System.Collections.Generic;
using Intrepid.Gameplay.Entities.Player.PDA.UI;
using Intrepid.Structures;
using UnityEngine;

namespace Intrepid.Gameplay.Entities.Player.PDA
{
    [Serializable]
    [CreateAssetMenu(fileName = "Application Library", menuName = "Intrepid/PDA/Application Library")]
    public class PDAApplicationLibraryData : ScriptableObject
    {
        [SerializeField] private List<PDAApplicationUI> applicationPrefabs;

        private SDictionary<PDAContentTag, PDAApplicationUI> ApplicationPrefabs { get; } = new();

        private void OnEnable()
        {
            ApplicationPrefabs.Clear();
            foreach (var prefab in applicationPrefabs)
            {
                ApplicationPrefabs.Add(prefab.ContentTag, prefab);
            }
        }

        public PDAApplicationUI Load(PDAContentTag contentTag, Transform parent)
        {
            if (!ApplicationPrefabs.TryGetValue(contentTag, out var prefab)) return null;
            var app = Instantiate(prefab, parent);
            app.gameObject.SetActive(false);
            return app;
        }

        public bool Contains(PDAContentTag contentTag)
        {
            if (contentTag.IsNull()) return false;
            return ApplicationPrefabs.ContainsKey(contentTag);
        }
    }
}