namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "dream_eater")]
public class DreamEater : Script, IScriptOnSecondaryEffect, IScriptBlockOutgoingHit
{
    /// <inheritdoc />
    public void BlockOutgoingHit(IExecutingMove executingMove, IPokemon target, byte hitIndex, ref bool block)
    {
        if (!target.HasStatus("asleep"))
            block = true;
    }

    /// <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 * 0.5f);
        if (move.User.HasHeldItem("big_root"))
            healed = (uint)(healed * 1.3f);

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

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