namespace PkmnLib.Plugin.Gen7.Scripts.Pokemon;

[Script(ScriptCategory.Pokemon, "stockpile_effect")]
public class StockpileEffect : Script, IScriptStack
{
    private IPokemon? _pokemon;
    public int StockpileCount { get; set; } = 1;

    /// <inheritdoc />
    public override void OnAddedToParent(IScriptSource source)
    {
        if (source is not IPokemon pokemon)
        {
            throw new InvalidOperationException("StockpileEffect can only be added to a Pokemon.");
        }
        _pokemon = pokemon;
        EventBatchId batchId = new();
        pokemon.ChangeStatBoost(Statistic.Defense, 1, true, false, batchId);
        pokemon.ChangeStatBoost(Statistic.SpecialDefense, 1, true, false, batchId);
        StockpileCount = 1;
    }

    /// <inheritdoc />
    public void Stack()
    {
        if (StockpileCount < 3)
        {
            EventBatchId batchId = new();
            _pokemon?.ChangeStatBoost(Statistic.Defense, 1, true, false, batchId);
            _pokemon?.ChangeStatBoost(Statistic.SpecialDefense, 1, true, false, batchId);
            StockpileCount++;
        }
    }

    /// <inheritdoc />
    public override void OnRemove()
    {
        if (_pokemon == null)
        {
            return;
        }
        EventBatchId batchId = new();
        _pokemon.ChangeStatBoost(Statistic.Defense, (sbyte)-StockpileCount, true, false, batchId);
        _pokemon.ChangeStatBoost(Statistic.SpecialDefense, (sbyte)-StockpileCount, true, false, batchId);
        StockpileCount = 0;
    }
}