using PkmnLib.Plugin.Gen7.Scripts.MoveVolatile;

namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "water_pledge")]
public class WaterPledge : Script, IScriptStopBeforeMove
{
    /// <inheritdoc />
    public void StopBeforeMove(IExecutingMove move, ref bool stop)
    {
        if (move.MoveChoice.Volatile.Contains<GrassWaterPledgeMove>() ||
            move.MoveChoice.Volatile.Contains<FireWaterPledgeMove>())
        {
            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 == "grass_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 == "grass_pledge")
        {
            pledgeMove.Volatile.Add(new GrassWaterPledgeMove());
        }
        else if (pledgeMove.ChosenMove.MoveData.Name == "fire_pledge")
        {
            pledgeMove.Volatile.Add(new FireWaterPledgeMove());
        }
    }
}