From 5a674872e6bfda1a459b92fd7f4e31b95543695b Mon Sep 17 00:00:00 2001
From: Evan Kranzler <theelk801@gmail.com>
Date: Wed, 1 Jun 2022 21:14:18 -0400
Subject: [PATCH] [CLB] Implemented Wild Magic Surge

---
 .../src/mage/cards/w/WildMagicSurge.java      | 93 +++++++++++++++++++
 .../CommanderLegendsBattleForBaldursGate.java |  1 +
 2 files changed, 94 insertions(+)
 create mode 100644 Mage.Sets/src/mage/cards/w/WildMagicSurge.java

diff --git a/Mage.Sets/src/mage/cards/w/WildMagicSurge.java b/Mage.Sets/src/mage/cards/w/WildMagicSurge.java
new file mode 100644
index 0000000000..64dfaadbc2
--- /dev/null
+++ b/Mage.Sets/src/mage/cards/w/WildMagicSurge.java
@@ -0,0 +1,93 @@
+package mage.cards.w;
+
+import mage.abilities.Ability;
+import mage.abilities.effects.OneShotEffect;
+import mage.cards.*;
+import mage.constants.CardType;
+import mage.constants.Outcome;
+import mage.constants.Zone;
+import mage.filter.StaticFilters;
+import mage.game.Game;
+import mage.game.permanent.Permanent;
+import mage.players.Player;
+import mage.target.TargetPermanent;
+
+import java.util.List;
+import java.util.UUID;
+
+/**
+ * @author TheElk801
+ */
+public final class WildMagicSurge extends CardImpl {
+
+    public WildMagicSurge(UUID ownerId, CardSetInfo setInfo) {
+        super(ownerId, setInfo, new CardType[]{CardType.INSTANT}, "{R}{R}");
+
+        // Destroy target permanent an opponent controls. Its controller reveals cards from the top of their library until they reveal a permanent card that shares a card type with that permanent. They put that card onto the battlefield and the rest on the bottom of their library in a random order.
+        this.getSpellAbility().addEffect(new WildMagicSurgeEffect());
+        this.getSpellAbility().addTarget(new TargetPermanent(StaticFilters.FILTER_OPPONENTS_PERMANENT));
+    }
+
+    private WildMagicSurge(final WildMagicSurge card) {
+        super(card);
+    }
+
+    @Override
+    public WildMagicSurge copy() {
+        return new WildMagicSurge(this);
+    }
+}
+
+class WildMagicSurgeEffect extends OneShotEffect {
+
+    WildMagicSurgeEffect() {
+        super(Outcome.Benefit);
+        staticText = "destroy target permanent an opponent controls. Its controller reveals cards " +
+                "from the top of their library until they reveal a permanent card that shares a " +
+                "card type with that permanent. They put that card onto the battlefield " +
+                "and the rest on the bottom of their library in a random order";
+    }
+
+    private WildMagicSurgeEffect(final WildMagicSurgeEffect effect) {
+        super(effect);
+    }
+
+    @Override
+    public WildMagicSurgeEffect copy() {
+        return new WildMagicSurgeEffect(this);
+    }
+
+    private static final Card loopCards(List<CardType> cardTypes, Player player, Cards cards, Ability source, Game game) {
+        for (Card card : player.getLibrary().getCards(game)) {
+            cards.add(card);
+            if (card.isPermanent(game)
+                    && card
+                    .getCardType(game)
+                    .stream()
+                    .anyMatch(cardTypes::contains)) {
+                return card;
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public boolean apply(Game game, Ability source) {
+        Permanent permanent = game.getPermanent(getTargetPointer().getFirst(game, source));
+        if (permanent == null) {
+            return false;
+        }
+        permanent.destroy(source, game);
+        Player player = game.getPlayer(permanent.getControllerId());
+        if (player == null) {
+            return false;
+        }
+        Cards cards = new CardsImpl();
+        Card card = loopCards(permanent.getCardType(game), player, cards, source, game);
+        player.revealCards(source, cards, game);
+        player.moveCards(card, Zone.BATTLEFIELD, source, game);
+        cards.retainZone(Zone.LIBRARY, game);
+        player.putCardsOnBottomOfLibrary(cards, game, source, false);
+        return true;
+    }
+}
diff --git a/Mage.Sets/src/mage/sets/CommanderLegendsBattleForBaldursGate.java b/Mage.Sets/src/mage/sets/CommanderLegendsBattleForBaldursGate.java
index 4f495ca709..c8b5dce58a 100644
--- a/Mage.Sets/src/mage/sets/CommanderLegendsBattleForBaldursGate.java
+++ b/Mage.Sets/src/mage/sets/CommanderLegendsBattleForBaldursGate.java
@@ -327,6 +327,7 @@ public final class CommanderLegendsBattleForBaldursGate extends ExpansionSet {
         cards.add(new SetCardInfo("Warehouse Thief", 205, Rarity.COMMON, mage.cards.w.WarehouseThief.class));
         cards.add(new SetCardInfo("Wayfarer's Bauble", 344, Rarity.COMMON, mage.cards.w.WayfarersBauble.class));
         cards.add(new SetCardInfo("White Plume Adventurer", 49, Rarity.RARE, mage.cards.w.WhitePlumeAdventurer.class));
+        cards.add(new SetCardInfo("Wild Magic Surge", 206, Rarity.UNCOMMON, mage.cards.w.WildMagicSurge.class));
         cards.add(new SetCardInfo("Wilson, Refined Grizzly", 261, Rarity.UNCOMMON, mage.cards.w.WilsonRefinedGrizzly.class));
         cards.add(new SetCardInfo("Winter Eladrin", 104, Rarity.COMMON, mage.cards.w.WinterEladrin.class));
         cards.add(new SetCardInfo("Wyll, Blade of Frontiers", 208, Rarity.RARE, mage.cards.w.WyllBladeOfFrontiers.class));