using PkmnLib.Plugin.Gen7.Scripts.Pokemon;

namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "heal_each_end_of_turn")]
public class HealEachEndOfTurn : Script, IScriptOnInitialize, IScriptOnSecondaryEffect
{
    private float _healPercentage;

    /// <inheritdoc />
    public void OnInitialize(IReadOnlyDictionary<StringKey, object?>? parameters)
    {
        if (parameters == null || !parameters.TryGetValue("percent", out var healPercentageObj) ||
            healPercentageObj is not float healPercentage)
        {
            _healPercentage = 6.25f;
        }
        else
        {
            _healPercentage = healPercentage;
        }
    }

    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
    {
        var effectName = ScriptUtils.ResolveName<HealEachEndOfTurnEffect>();
        if (target.Volatile.Contains(effectName))
        {
            move.GetHitData(target, hit).Fail();
            return;
        }
        target.Volatile.Add(new HealEachEndOfTurnEffect(_healPercentage, target));
    }
}