using PkmnLib.Plugin.Gen7.Scripts.Utils;

namespace PkmnLib.Plugin.Gen7.Scripts.Pokemon;

[Script(ScriptCategory.Pokemon, "ice_ball")]
public class IceBallEffect : Script, IScriptForceTurnSelection, IScriptOnMoveMiss, IScriptOnEndTurn
{
    private readonly IPokemon _owner;
    private readonly StringKey _moveName;
    public int TurnCount { get; set; }

    public IceBallEffect(IPokemon owner, StringKey moveName)
    {
        _owner = owner;
        _moveName = moveName;
        TurnCount = 0;
    }

    /// <inheritdoc />
    public void ForceTurnSelection(IBattle battle, byte sideIndex, byte position, ref ITurnChoice? choice)
    {
        var opposingSideIndex = (byte)(_owner.BattleData?.SideIndex == 0 ? 1 : 0);
        choice = TurnChoiceHelper.CreateMoveChoice(_owner, _moveName, opposingSideIndex, position);
    }

    /// <inheritdoc />
    public void OnMoveMiss(IExecutingMove move, IPokemon target)
    {
        RemoveSelf();
    }

    /// <inheritdoc />
    public void OnEndTurn(IScriptSource owner, IBattle battle)
    {
        if (TurnCount < 5)
            TurnCount++;
        else
            RemoveSelf();
    }
}