using PkmnLib.Plugin.Gen7.Scripts.Pokemon;

namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;

/// <summary>
/// Inner Focus is an ability that prevents the Pokémon from flinching.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Inner_Focus_(Ability)">Bulbapedia - Inner Focus</see>
/// </summary>
[Script(ScriptCategory.Ability, "inner_focus")]
public class InnerFocus : Script, IScriptPreventVolatileAdd
{
    private IPokemon? _pokemon;

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

    /// <inheritdoc />
    public void PreventVolatileAdd(IScriptSource parent, Script script, ref bool preventVolatileAdd)
    {
        if (script is not FlinchEffect)
            return;
        _pokemon?.BattleData?.Battle.EventHook.Invoke(new AbilityTriggerEvent(_pokemon));
        preventVolatileAdd = true;
    }
}