using PkmnLib.Plugin.Gen7.Scripts.MoveVolatile;

namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "grass_pledge")]
public class GrassPledge : Script, IScriptStopBeforeMove
{
    /// <inheritdoc />
    public void StopBeforeMove(IExecutingMove move, ref bool stop)
    {
        if (move.MoveChoice.Volatile.Contains<GrassWaterPledgeMove>() ||
            move.MoveChoice.Volatile.Contains<FireGrassPledgeMove>())
        {
            return;
        }

        var pledgeMove = (IMoveChoice?)move.Battle.ChoiceQueue?.FirstOrDefault(x =>
            x is IMoveChoice mc && mc.User.BattleData?.SideIndex == move.User.BattleData?.SideIndex &&
            (mc.ChosenMove.MoveData.Name == "water_pledge" || mc.ChosenMove.MoveData.Name == "fire_pledge"));
        if (pledgeMove is null)
            return;

        // If a pledge move is already queued, we stop the current move.
        stop = true;

        if (pledgeMove.ChosenMove.MoveData.Name == "water_pledge")
        {
            pledgeMove.Volatile.Add(new GrassWaterPledgeMove());
        }
        else if (pledgeMove.ChosenMove.MoveData.Name == "fire_pledge")
        {
            pledgeMove.Volatile.Add(new FireGrassPledgeMove());
        }
    }
}