namespace PkmnLib.Plugin.Gen7.Scripts.Abilities;

/// <summary>
/// Friend Guard is an ability that reduces the damage taken by allies by 25%.
/// This ability is commonly associated with Clefairy and Vivillon.
///
/// <see href="https://bulbapedia.bulbagarden.net/wiki/Friend_Guard_(Ability)">Bulbapedia - Friend Guard</see>
/// </summary>
[Script(ScriptCategory.Ability, "friend_guard")]
public class FriendGuard : Script
{
    private IPokemon? _pokemon;

    /// <inheritdoc />
    public override void OnAddedToParent(IScriptSource source)
    {
        if (source is not IPokemon pokemon)
            throw new InvalidOperationException("Friend Guard can only be added to a Pokemon script source.");
        _pokemon = pokemon;
        var effect = _pokemon.BattleData?.BattleSide.VolatileScripts.Add(new Side.FriendGuardEffect());
        (effect?.Script as Side.FriendGuardEffect)?.OnAdded(_pokemon);
    }

    /// <inheritdoc />
    public override void OnRemove()
    {
        if (_pokemon is null)
            return;
        if (_pokemon.BattleData?.BattleSide.VolatileScripts.TryGet<Side.FriendGuardEffect>(out var script) == true)
        {
            script.OnRemoved(_pokemon);
        }
    }
}