#!/usr/bin/env python

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


###############################################################################
class Card_Count(Card.Card):
    def __init__(self):
        Card.Card.__init__(self)
        self.cardtype = Card.CardType.ACTION
        self.base = Card.CardExpansion.DARKAGES
        self.desc = """Choose one: Discard 2 cards; or put a card from your hand
            on top of your deck; or gain a Copper. Choose one: +3 Coin; or trash
            your hand; or gain a Duchy"""
        self.name = "Count"
        self.cost = 5

    ###########################################################################
    def special(self, game, player):
        """Choose one: Discard 2 cards; or put a card from your
        hand on top of your deck; or gain a copper.

        Choose one: +3 coin, or trash your hand or gain a Duchy"""

        ans = player.plr_choose_options(
            "What do you want to do?",
            ("Discard 2 cards", "discard"),
            ("Put a card from you hand on top of your deck", "put_card"),
            ("Gain a copper", "copper"),
        )
        if ans == "copper":
            player.output("Gained a copper")
            player.gain_card("Copper")
        elif ans == "put_card":
            self.put_card(game, player)
        else:
            player.plr_discard_cards(2)

        ans = player.plr_choose_options(
            "What do you want to do now?",
            ("+3 coin", "coin"),
            ("Trash hand", "trash"),
            ("Gain Duchy", "duchy"),
        )
        if ans == "duchy":
            player.output("Gained a duchy")
            player.gain_card("Duchy")
        elif ans == "trash":
            for card in player.piles[Piles.HAND]:
                player.output(f"Trashing {card.name}")
                player.trash_card(card)
        else:
            player.coins.add(3)

    ###########################################################################
    def put_card(self, _, player):
        """Put a card from your hand on top of your deck"""
        index = 1
        options = []
        for card in player.piles[Piles.HAND]:
            pr = f"Put {card.name} on top of your deck"
            options.append({"selector": f"{index}", "print": pr, "card": card})
            index += 1
        o = player.user_input(options, "Select card to put on top of your deck")
        player.output(f"Moving {o['card'].name} to top of deck")
        player.move_card(o["card"], "topdeck")


###############################################################################
class TestCount(unittest.TestCase):
    def setUp(self):
        self.g = Game.TestGame(numplayers=1, initcards=["Count"], badcards=["Duchess"])
        self.g.start_game()
        self.plr = self.g.player_list(0)
        self.card = self.g["Count"].remove()
        self.plr.piles[Piles.HAND].set("Copper", "Estate", "Silver", "Province", "Gold")

    def test_discard(self):
        self.plr.add_card(self.card, Piles.HAND)
        # Discard, select card 1 and card 2, finish selecting, +3 coin
        self.plr.test_input = [
            "discard 2",
            "discard estate",
            "discard copper",
            "finish",
            "+3 coin",
        ]
        self.plr.play_card(self.card)
        self.assertEqual(self.plr.piles[Piles.DISCARD].size(), 2)
        self.assertEqual(self.plr.piles[Piles.HAND].size(), 3)

    def test_top_deck(self):
        self.plr.piles[Piles.HAND].set("Gold")
        self.plr.add_card(self.card, Piles.HAND)
        # top deck, card select, +3 coin
        self.plr.test_input = ["top of your deck", "put gold", "+3 coin"]
        self.plr.play_card(self.card)
        nc = self.plr.next_card()
        self.assertEqual(nc.name, "Gold")

    def test_gain_copper(self):
        self.plr.add_card(self.card, Piles.HAND)
        self.plr.test_input = ["gain a copper", "+3 coin"]
        self.plr.play_card(self.card)
        self.assertEqual(self.plr.piles[Piles.DISCARD][0].name, "Copper")

    def test_gain_gold(self):
        self.plr.add_card(self.card, Piles.HAND)
        self.plr.test_input = ["gain a copper", "+3 coin"]
        self.plr.play_card(self.card)
        self.assertEqual(self.plr.coins.get(), 3)

    def test_trash_hand(self):
        tsize = self.g.trashpile.size()
        self.plr.add_card(self.card, Piles.HAND)
        self.plr.test_input = ["gain a copper", "trash hand"]
        self.plr.play_card(self.card)
        self.assertTrue(self.plr.piles[Piles.HAND].is_empty())
        self.assertEqual(self.g.trashpile.size(), tsize + 5)

    def test_gain_duchy(self):
        self.plr.add_card(self.card, Piles.HAND)
        self.plr.test_input = ["gain a copper", "gain duchy"]
        self.plr.play_card(self.card)
        self.assertIn("Duchy", self.plr.piles[Piles.DISCARD])


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

# EOF
