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

namespace NanoPlay.Logic
{
    public static class Utils
    {
        public static ICollection<T> DirsToList<T>(string path, Func<string, T> mapping) =>
            Directory.EnumerateDirectories(path).Select(mapping).ToList();

        public static ICollection<T> FilesToList<T>(string path, Func<string, T> mapping) =>
            Directory.EnumerateFiles(path).Select(mapping).ToList();

        private static readonly Random Random = new Random();

        public static IEnumerable<T> Randomize<T>(this IEnumerable<T> collection)
        {
            return collection.OrderBy(x => Random.Next());
        }

        public static ICollection<T> RandomSubset<T>(this IEnumerable<T> collection, int count)
        {
            return collection.Randomize().Take(count).ToList();
        }

        public static T PickRandom<T>(this IEnumerable<T> collection)
        {
            return collection.Randomize().First();
        }
    }
}