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

public static class MiscExtensions
{
    public static T GetRandom<T>(this List<T> list, int startInd, int endInd, Func<T, bool> condition = null)
    {
        var checkList = list;
        if (condition != null)
            checkList = list.Where(condition).ToList();
        return checkList.Count == 0
            ? default(T)
            : checkList[Random.Range(Mathf.Clamp(startInd, 0, checkList.Count), Mathf.Clamp(endInd, 0, checkList.Count))];
    }

    public static T GetRandom<T>(this List<T> list, Func<T, bool> condition = null)
    {
        return list.GetRandom(0, list.Count, condition);
    }

    public static void FitListToList<T1, T2>(this List<T1> list1, List<T2> list2) where T1 : new()
    {
        // adding missing
        var startInd = list1.Count;
        for (int i = startInd; i < list2.Count; i++)
            list1.Add(new T1());

        // removing redundant
        for (int i = 0; i < list1.Count - list2.Count; i++)
            list1.RemoveAt(list1.Count - 1);
    }

    public static Color SetAlpha(this Color c, float a)
    {
        c.a = a;
        return c;
    }

    public static string SetColorTag(this string s, string color)
    {
        return string.IsNullOrEmpty(s) ? s : $"<color={color}>{s}</color>";
    }
}