using System.Text.Json;
using PkmnLib.Static;
using PkmnLib.Static.Libraries;

namespace PkmnLib.Dynamic.Libraries.DataLoaders;

/// <summary>
/// Loads growth rate data from a JSON file.
/// </summary>
public static class GrowthRateDataLoader
{
    /// <summary>
    /// Loads the growth rate library from a JSON file.
    /// </summary>
    public static GrowthRateLibrary LoadGrowthRates(Stream stream, Action<List<IGrowthRate>>? action = null)
    {
        var objects = JsonSerializer.Deserialize<Dictionary<string, uint[]>>(stream, JsonOptions.DefaultOptions)!;
        var growthRates = objects.Select(x => new LookupGrowthRate(x.Key, x.Value)).Cast<IGrowthRate>().ToList();
        action?.Invoke(growthRates);

        var library = new GrowthRateLibrary();
        foreach (var growthRate in growthRates)
        {
            library.Add(growthRate);
        }
        return library;
    }
}