namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "recoil")]
public class Recoil : Script, IScriptOnInitialize, IScriptOnSecondaryEffect
{
    private float _recoilPercentage;

    /// <inheritdoc />
    public void OnInitialize(IReadOnlyDictionary<StringKey, object?>? parameters)
    {
        if (parameters == null)
            return;
        if (parameters.TryGetValue("recoil_percentage", out var recoilPercentage))
            _recoilPercentage = recoilPercentage as float? ?? 0f;
    }

    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
    {
        if (_recoilPercentage <= 0)
            return;
        var hitData = move.GetHitData(target, hit);
        var recoilDamage = (uint)(hitData.Damage * _recoilPercentage);

        var triggerArgs = new CustomTriggers.ModifyRecoilArgs(move, target, hit, hitData.Damage, recoilDamage);
        move.RunScriptHook<IScriptCustomTrigger>(x => x.CustomTrigger(CustomTriggers.ModifyRecoil, triggerArgs));
        if (triggerArgs.Prevent)
            return;

        move.User.Damage(recoilDamage, DamageSource.Misc);
    }
}