namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;

/// <summary>
/// Frisk is an ability that reveals the held items of opposing Pokémon when the Pokémon enters battle.
/// This ability is commonly associated with Banette and Gothitelle.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Frisk_(Ability)">Bulbapedia - Frisk</see>
/// </summary>
[Script(ScriptCategory.Ability, "frisk")]
public class Frisk : Script, IScriptOnSwitchIn
{
    /// <inheritdoc />
    public void OnSwitchIn(IPokemon pokemon, byte position)
    {
        if (pokemon.BattleData?.BattleSide is null)
            return;

        // Check if the Pokémon has an opposing side
        var opposingSide =
            pokemon.BattleData.Battle.Sides.FirstOrDefault(side => side != pokemon.BattleData.BattleSide);
        if (opposingSide is null)
            return;

        EventBatchId batchId = new();
        // Iterate through the opposing Pokémon
        foreach (var opponent in opposingSide.Pokemon.WhereNotNull())
        {
            // If the opponent has a held item, reveal it
            if (opponent.HeldItem != null)
            {
                pokemon.BattleData.Battle.EventHook.Invoke(new AbilityTriggerEvent(pokemon)
                {
                    Metadata = new Dictionary<StringKey, object?>
                    {
                        { "opponent", opponent },
                        { "held_item", opponent.HeldItem },
                    },
                    BatchId = batchId,
                });
            }
        }
    }
}