using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.IO;
using System;

#if UNITY_EDITOR
using UnityEditor;
#endif

/// <summary>
/// cette classe sert à réunir les prefabs et les scriptable objects importants. <br/>
/// Elle permet d'éviter de devoir mettre tous nos prefabs et scriptable objects dans le dossier Resources <br/>
/// et permet d'accéder aux éléments qu'on cherche par leur nom directement
/// </summary>
public class Database : SingletonBehaviour<Database>
{
#if UNITY_EDITOR
    [Header("Editor section")]
    [SerializeField]
    private List<DefaultAsset> _folders;

    public List<DefaultAsset> Folders => _folders;
#endif

    public const string TYPE = nameof(TYPE);

    [Serializable]
    public class DataList<T> : IEnumerable<T> where T : UnityEngine.Object
    {
        [SerializeField]
        private List<T> elements;
        public DataList() => elements = new List<T>();

        public T this[string key] => elements.Find(x => x.name == key);

        public void Add(T element) => elements.Add(element);

        public void Clear() => elements.Clear();
        public IEnumerator<T> GetEnumerator()
        {
            return elements.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return elements.GetEnumerator();
        }

        public static implicit operator DataList<T>(List<T> list) => new DataList<T>() { elements = list };
    }

    [SerializeField]
    private List<GameObject> _prefabs;
    [SerializeField]
    private List<ScriptableObject> _scriptableObjects;

    public DataList<GameObject> Prefabs => _prefabs;
    public DataList<ScriptableObject> ScriptableObjects => _scriptableObjects;

    public Database()
    {
        _prefabs = new List<GameObject>();
        _scriptableObjects = new List<ScriptableObject>();
    }
#if UNITY_EDITOR
    
#endif
}