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

[System.Serializable]
public class LibraryEntry
{
    public string Name;
    public Transform Prefab;
    public int MinDepth;
    public int MaxDepth;
    public int EarlyOutOfDepth;
    public float Weight;
}

[System.Serializable]
public class PrefabLibrary {

    public List<LibraryEntry> Entries;

    public Transform GetEntry(int depth)
    {
        var cands = Entries.Where(e => e.MinDepth <= depth && depth <= e.MaxDepth);
        List<LibraryEntry> list = new List<LibraryEntry>(cands);
        return WeightedBag.RandomElementByWeight(list, e => e.Weight).Prefab;
    }

    public Transform GetOutOfDepthEntry(int depth)
    {
        // return an entry that wouldn't normally qualify but is okay because of EarlyOutOfDepth
        var cands = Entries.Where(e => e.EarlyOutOfDepth <= depth && depth < e.MinDepth);
        List<LibraryEntry> list = new List<LibraryEntry>(cands);
        LibraryEntry entry = ListHelper.DrawFromAtRandom(list);
        if (entry != null)
            return entry.Prefab;
        else
            return GetEntry(depth);
    }

}
