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

public static class RandomUtilities
{
    // Selects n_choose out of n_max indices, non-repeating
    public static List<int> RouletteSelectNonRepeating(int n_choose, int n_max)
    {
        List<int> chosen_indices = new List<int>(n_choose);
        List<int> valid_indices = new List<int>(n_max);
        for (int i = 0; i < n_max; i++)
        {
            valid_indices.Add(i);
            //Debug.Log("VALID " + i);
        }

        for (int c = 0; c < n_choose; c++)
        {
            //Debug.Log("TEST " + c);
            int v = Random.Range(0, valid_indices.Count);
            //Debug.Log("INDEX V " + v);
            chosen_indices.Add(valid_indices[v]);
            //Debug.Log("CHOSEN " + valid_indices[v]);
            valid_indices.RemoveAt(v);
        }
        return chosen_indices;
    }


    public static List<T> RouletteSelectNonRepeating<T>(System.Func<int, T> select, int n_choose, int n_max)
    {
        List<int> indices = RouletteSelectNonRepeating(n_choose, n_max);
        
        List<T> list = new List<T>();
        foreach (int i in indices)
        {
            list.Add(select(i));
            //Debug.Log("SELECTED " + i);
        }
        return list;
    }

    public static T UniformFromList<T>(T[] list){
        return list[Random.Range(0, list.Length)];
    }

    public static T UniformFromList<T>(List<T> list)
    {
        if (list.Count == 0) return default(T);
        return list[Random.Range(0, list.Count)];
    }
}