}
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
MageObject sourceObject = game.getObject(source.getSourceId());
if (controller != null && sourceObject != null) {
FilterCard filter = new FilterCard("card in your hand to exile");
FilterCard filter2 = new FilterCard("(move the window) card exiled by " + sourceObject.getLogName() + " to put on top of library");
TargetCardInHand target = new TargetCardInHand(0, controller.getHand().size(), filter);
target.setRequired(false);
int amountExiled = 0;
if (target.canChoose(source.getControllerId(), game) && target.choose(Outcome.Neutral, source.getControllerId(), source.getSourceId(), game)) {
if (!target.getTargets().isEmpty()) {
List<UUID> targets = target.getTargets();
for (UUID targetId : targets) {
Card card = game.getCard(targetId);
if (card != null) {
card.setFaceDown(true);
if (card.moveToExile(source.getSourceId(), sourceObject.getLogName(), source.getSourceId(), game)) {
amountExiled++;
}
}
}
}
}
game.informPlayers(controller.getName() + " exiles " + amountExiled + " card" + (amountExiled == 1 ? " ":"s ") + "face down from his or her hand");
if (amountExiled > 0) {
int count = Math.min(controller.getLibrary().size(), amountExiled);
for (int i = 0; i < count; i++) {
Card card = controller.getLibrary().removeFromTop(game);
if (card != null) {
card.moveToZone(Zone.HAND, source.getSourceId(), game, false);
}
}
}
game.informPlayers(controller.getName() + " moves " + amountExiled + " card" + (amountExiled == 1 ? " ":"s ") + "from library to hand");
TargetCardInExile target2 = new TargetCardInExile(filter2, source.getSourceId());
ExileZone scrollRackExileZone = game.getExile().getExileZone(source.getSourceId());
if (scrollRackExileZone != null) {
while (controller.isInGame() && scrollRackExileZone.count(filter, game) > 1) {
controller.lookAtCards("exiled cards with " + sourceObject.getLogName(), scrollRackExileZone, game);
controller.choose(Outcome.Neutral, scrollRackExileZone, target2, game);
Card card = game.getCard(target2.getFirstTarget());
if (card != null) {
game.getExile().removeCard(card, game);
controller.moveCardToLibraryWithInfo(card, source.getSourceId(), game, Zone.EXILED, true, false);