namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "change_multiple_user_stat_boosts")]
public class ChangeMultipleUserStatBoosts : Script, IScriptOnInitialize, IScriptOnSecondaryEffect
{
    private Dictionary<Statistic, sbyte> _statBoosts = new();

    /// <inheritdoc />
    public void OnInitialize(IReadOnlyDictionary<StringKey, object?>? parameters)
    {
        if (parameters == null)
        {
            throw new ArgumentNullException(nameof(parameters));
        }

        foreach (var par in parameters)
        {
            if (!Enum.TryParse<Statistic>(par.Key, true, out var stat))
                throw new ArgumentException($"Invalid stat name: {par.Key}");
            if (par.Value is sbyte value)
            {
                _statBoosts[stat] = value;
            }
        }
    }

    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
    {
        EventBatchId batchId = new();
        foreach (var stat in _statBoosts)
        {
            move.User.ChangeStatBoost(stat.Key, stat.Value, true, false, batchId);
        }
    }
}