}
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
Cards cards = new CardsImpl(Zone.PICK);
int count = Math.min(player.getLibrary().size(), 4);
for (int i = 0; i < count; i++) {
Card card = player.getLibrary().removeFromTop(game);
cards.add(card);
game.setZone(card.getId(), Zone.PICK);
}
Permanent hideawaySource = game.getPermanent(source.getSourceId());
if (cards.size() == 0 || hideawaySource == null) {
return false;
}
TargetCard target1 = new TargetCard(Zone.PICK, filter1);
if (player.choose(Outcome.Detriment, cards, target1, game)) {
Card card = cards.get(target1.getFirstTarget(), game);
if (card != null) {
cards.remove(card);
card.setFaceDown(true);
card.moveToExile(CardUtil.getCardExileZoneId(game, source),
new StringBuilder("Hideaway (").append(hideawaySource.getName()).append(")").toString(),
source.getSourceId(), game);
}
target1.clearChosen();
}
if (cards.size() > 0) {
TargetCard target2 = new TargetCard(Zone.PICK, filter2);
while (player.isInGame() && cards.size() > 1) {
player.choose(Outcome.Benefit, cards, target2, game);
Card card = cards.get(target2.getFirstTarget(), game);
if (card != null) {
cards.remove(card);
card.moveToZone(Zone.LIBRARY, source.getSourceId(), game, false);
}
target2.clearChosen();
}
Card card = cards.get(cards.iterator().next(), game);
card.moveToZone(Zone.LIBRARY, source.getSourceId(), game, false);
}
return true;
}