namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;

/// <summary>
/// Innards Out is an ability that deals damage to the attacker equal to the amount of HP lost when the Pokémon with this ability faints.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Innards_Out_(Ability)">Bulbapedia - Innards Out</see>
/// </summary>
[Script(ScriptCategory.Ability, "innards_out")]
public class InnardsOut : Script, IScriptOnIncomingHit, IScriptOnDamage
{
    private IPokemon? _lastPokemonToHit;

    /// <inheritdoc />
    public void OnIncomingHit(IExecutingMove move, IPokemon target, byte hit)
    {
        _lastPokemonToHit = move.User;
    }

    /// <inheritdoc />
    public void OnDamage(IPokemon pokemon, DamageSource source, uint oldHealth, uint newHealth)
    {
        if (newHealth != 0 || source is not DamageSource.MoveDamage)
            return;
        if (_lastPokemonToHit is null || !_lastPokemonToHit.IsUsable)
            return;

        EventBatchId batchId = new();

        pokemon.BattleData?.Battle.EventHook.Invoke(new AbilityTriggerEvent(pokemon)
        {
            BatchId = batchId,
        });
        _lastPokemonToHit.Damage(oldHealth, DamageSource.Misc, batchId);
    }
}