#!/usr/bin/env python

import unittest
from dominion import Game, Card, Piles
import dominion.Card as Card


###############################################################################
class Card_Mystic(Card.Card):
    def __init__(self):
        Card.Card.__init__(self)
        self.cardtype = Card.CardType.ACTION
        self.base = Card.CardExpansion.DARKAGES
        self.desc = "+2 coin, +1 action; Name a card. Reveal the top card of your deck. If it's the named card, put it into your hand."
        self.name = "Mystic"
        self.actions = 1
        self.coin = 2
        self.cost = 5

    ###########################################################################
    def special(self, game, player):
        """ " Name a card. Reveal the top card of your deck. If it's
        the named card, put it into your hand"""
        options = [{"selector": "0", "print": "No guess", "card": None}]
        index = 1
        for c in sorted(game.cardTypes()):
            sel = "%s" % index
            options.append({"selector": sel, "print": "Guess %s" % c.name, "card": c})
            index += 1
        o = player.user_input(options, "Guess the top card")
        if not o["card"]:
            return
        c = player.next_card()
        player.reveal_card(c)
        if o["card"].name == c.name:
            player.output("You guessed correctly")
            player.add_card(c, Piles.HAND)
        else:
            player.output("You chose poorly - it was a %s" % c.name)
            player.add_card(c, "topdeck")


###############################################################################
class Test_Mystic(unittest.TestCase):
    def setUp(self):
        self.g = Game.TestGame(
            numplayers=1,
            initcards=["Mystic"],
            badcards=["Tournament", "Fool's Gold", "Pooka"],
        )
        self.g.start_game()
        self.plr = self.g.player_list(0)
        self.card = self.g["Mystic"].remove()

    def test_play(self):
        """No guess should still get results"""
        self.plr.add_card(self.card, Piles.HAND)
        self.plr.test_input = ["0"]
        self.plr.play_card(self.card)
        self.assertEqual(self.plr.actions.get(), 1)
        self.assertEqual(self.plr.coins.get(), 2)

    def test_good(self):
        """When the guess is good the card should move to the hand"""
        self.plr.add_card(self.card, Piles.HAND)
        self.plr.piles[Piles.DECK].set("Province")
        self.plr.test_input = ["Province"]
        self.plr.play_card(self.card)
        self.assertEqual(self.plr.actions.get(), 1)
        self.assertEqual(self.plr.coins.get(), 2)
        self.assertIn("Province", self.plr.piles[Piles.HAND])
        self.assertTrue(self.plr.piles[Piles.DECK].is_empty())

    def test_bad(self):
        """When the guess is bad the card should stay on the deck"""
        self.plr.add_card(self.card, Piles.HAND)
        self.plr.piles[Piles.DECK].set("Province")
        self.plr.test_input = ["Gold"]
        self.plr.play_card(self.card)
        self.assertEqual(self.plr.actions.get(), 1)
        self.assertEqual(self.plr.coins.get(), 2)
        self.assertNotIn("Gold", self.plr.piles[Piles.HAND])
        self.assertNotIn("Province", self.plr.piles[Piles.HAND])
        self.assertEqual(self.plr.piles[Piles.DECK][-1].name, "Province")


###############################################################################
if __name__ == "__main__":  # pragma: no cover
    unittest.main()

# EOF
