using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using ScrambleKit.Extensions;

namespace ScrambleKit.ProcGen
{
    [System.Serializable]
    public class LibraryEntry
    {
        public Transform Prefab;
        public int EarliestLevel;
        public int LastLevel;
        public float PickWeight;
    }

    [CreateAssetMenu]
    public class SpawnLibrary : ScriptableObject
    {

        public List<LibraryEntry> Entries;

        public Transform PickPrefab(int Level)
        {
            try
            {
                LibraryEntry entry = Entries
                                    .Where(o => Level >= o.EarliestLevel)
                                    .Where(q => Level <= q.LastLevel)
                                    .DrawFromAtRandomByWeight(e => e.PickWeight);

                return entry.Prefab;
            }
            catch(System.Exception)
            {
                Debug.LogError("Error: SpawnLibrary " + name + " was unable to find an entry for Level " + Level);
                return null;
            }
        }
    }
}
