from unittest import TestCase

from server.model.game import Game
from server.model.players import MenteurPlayer, NaivePlayer, RandomPlayer


class TestGame(TestCase):
    def setUp(self) -> None:
        super().setUp()
        self.player1 = RandomPlayer("PLN")
        self.player2 = NaivePlayer("Nassim")
        self.game = Game([self.player1, self.player2])

    def test_global_hand(self) -> None:
        card1 = self.game.deck.random_card()
        self.player1.give(card1)

        card2 = self.game.deck.random_card()
        card3 = self.game.deck.random_card()
        self.player2.give(card2)
        self.player2.give(card3)

        self.assertTrue(card1 in self.game.global_hand, "Global hand should contain player1's card")
        self.assertTrue(card2 in self.game.global_hand, "Global hand should contain player2's first card")
        self.assertTrue(card3 in self.game.global_hand, "Global hand should contain player2's second card")

    def test_add_player(self):
        self.game.add_player(RandomPlayer("Foo"))
        self.assertEqual(3, len(self.game.players), "Should be added")
        self.game.add_player(RandomPlayer("Foo"))
        self.assertEqual(3, len(self.game.players), "Should not add duplicate")

    async def test_turn(self):
        menteur = MenteurPlayer()
        naive = NaivePlayer()
        self.game = Game([menteur, naive])
        await self.game.new_turn()

        self.assertEqual(1, len([v for v in self.game.defeats.values() if v > 0]), "There should have been one defeat.")
        self.assertTrue(1 in self.game.defeats.values(), "A player should have one defeat.")

        loser = [p for p in self.game.players if self.game.defeats[p]][0]
        self.assertEqual(self.game.players[0], loser, "The loser should be first to play.")

    async def test_full_game(self):
        await self.game.new_game()

        self.assertGreater(len(self.game.defeats.values()), 0, "There should be at least one player with defeats.")
        self.assertTrue(5 in self.game.defeats.values(), "A player should have lost five times.")
