using PkmnLib.Static.Species;

namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;

/// <summary>
/// Disguise is an ability that allows the Pokémon to avoid damage from one attack.
/// After being hit, the disguise is broken and the Pokémon's appearance changes.
/// This ability is exclusive to Mimikyu.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Disguise_(Ability)">Bulbapedia - Disguise</see>
/// </summary>
[Script(ScriptCategory.Ability, "disguise")]
public class Disguise : Script, IScriptChangeIncomingDamage
{
    /// <inheritdoc />
    public void ChangeIncomingDamage(IPokemon pokemon, DamageSource source, ref uint damage)
    {
        if (pokemon.BattleData == null)
            return;
        if (source is not DamageSource.MoveDamage and not DamageSource.Confusion)
            return;
        if (pokemon.Form.Name == "busted" || pokemon.Form.Name == "totem-busted")
            return;
        IForm form;
        if (pokemon.Form.Name == "default")
        {
            if (!pokemon.Species.TryGetForm("busted", out form!))
                return;
        }
        else if (pokemon.Form.Name == "totem-disguised")
        {
            if (!pokemon.Species.TryGetForm("totem-busted", out form!))
                return;
        }
        else
        {
            return;
        }

        EventBatchId batchId = new();

        pokemon.BattleData.Battle.EventHook.Invoke(new AbilityTriggerEvent(pokemon)
        {
            BatchId = batchId,
        });
        pokemon.ChangeForm(form, batchId);

        damage = 0;
    }
}