namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;

/// <summary>
/// Shed Skin is an ability that gives the Pokémon a chance to heal from a status condition at the end of each turn.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Shed_Skin_(Ability)">Bulbapedia - Shed Skin</see>
/// </summary>
[Script(ScriptCategory.Ability, "shed_skin")]
public class ShedSkin : Script, IScriptOnEndTurn
{
    private IPokemon? _owningPokemon;

    /// <inheritdoc />
    public override void OnAddedToParent(IScriptSource source)
    {
        if (source is not IPokemon pokemon)
            throw new ArgumentException("ShedSkin script must be added to a Pokemon.", nameof(source));
        _owningPokemon = pokemon;
    }

    /// <inheritdoc />
    public void OnEndTurn(IScriptSource owner, IBattle battle)
    {
        if (_owningPokemon is null || _owningPokemon.StatusScript.IsEmpty)
            return;

        if (battle.Random.GetInt(3) == 0)
        {
            EventBatchId batchId = new();
            battle.EventHook.Invoke(new AbilityTriggerEvent(_owningPokemon)
            {
                BatchId = batchId,
            });
            _owningPokemon.ClearStatus(batchId);
        }
    }
}