namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "drain")]
public class Drain : Script, IScriptOnInitialize, IScriptOnSecondaryEffect
{
    public float DrainModifier { get; set; } = 0.5f;

    /// <inheritdoc />
    public void OnInitialize(IReadOnlyDictionary<StringKey, object?>? parameters)
    {
        if (parameters == null)
            return;
        DrainModifier = parameters.GetValueOrDefault("drain_modifier") as float? ?? DrainModifier;
    }

    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
    {
        var user = move.User;
        var damage = move.GetHitData(target, hit).Damage;
        var healed = (uint)(damage * DrainModifier);
        if (move.User.HasHeldItem("big_root"))
            healed = (uint)(healed * 1.3f);
        var invert = false;

        var args = new CustomTriggers.ModifyDrainArgs(move, target, hit, damage, healed, invert);
        target.RunScriptHook<IScriptCustomTrigger>(x => x.CustomTrigger(CustomTriggers.ModifyDrain, args));
        invert = args.Invert;
        healed = args.Healed;
        if (healed == 0)
            healed = 1;

        if (invert)
        {
            user.Damage(healed, DamageSource.Misc);
        }
        else
        {
            user.Heal(healed);
        }
    }
}