namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;

/// <summary>
/// Hydration is an ability that heals status conditions if it is raining at the end of the turn.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Hydration_(Ability)">Bulbapedia - Hydration</see>
/// </summary>
[Script(ScriptCategory.Ability, "hydration")]
public class Hydration : Script, IScriptOnEndTurn
{
    private IPokemon? _pokemon;

    /// <inheritdoc />
    public override void OnAddedToParent(IScriptSource source)
    {
        if (source is not IPokemon pokemon)
            throw new InvalidOperationException("Hydration can only be added to a Pokemon script source.");
        _pokemon = pokemon;
    }

    /// <inheritdoc />
    public void OnEndTurn(IScriptSource owner, IBattle battle)
    {
        if (_pokemon is null)
            return;

        if (battle.WeatherName != ScriptUtils.ResolveName<Weather.Rain>())
            return;
        if (_pokemon.StatusScript.IsEmpty)
            return;

        EventBatchId batchId = new();

        battle.EventHook.Invoke(new AbilityTriggerEvent(_pokemon)
        {
            BatchId = batchId,
        });
        _pokemon.ClearStatus(batchId);
    }
}