namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;

/// <summary>
/// RKS System is an ability that changes the Pokémon's type based on its held Memory.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/RKS_System_(Ability)">Bulbapedia - RKS System</see>
/// </summary>
[Script(ScriptCategory.Ability, "rks_system")]
public class RksSystem : Script, IScriptOnAfterHeldItemChange
{
    /// <inheritdoc />
    public void OnAfterHeldItemChange(IPokemon pokemon, IItem? previous, IItem? item)
    {
        if (pokemon.Species.Name != "silvally")
            return;
        if (item is null && pokemon.Form.Name != "default")
        {
            pokemon.ChangeForm(pokemon.Species.GetDefaultForm());
        }
        else if (item is not null && item.Name.ToString().EndsWith("_memory", StringComparison.OrdinalIgnoreCase))
        {
            var memoryPrefix =
                item.Name.ToString().Replace("_memory", string.Empty, StringComparison.OrdinalIgnoreCase);
            var formName = $"silvally_{memoryPrefix}";
            if (pokemon.Species.TryGetForm(formName, out var form))
            {
                pokemon.ChangeForm(form);
            }
        }
    }
}