import unittest
from datetime import datetime

from src.ezgg_lan_manager.types.TournamentBase import ParticipantType
from src.ezgg_lan_manager.types.Tournament import generate_new_tournament, GameTitle, TournamentFormat, TournamentStatus, TournamentError, Participant, MatchStatus


class TournamentDomainTests(unittest.TestCase):
    def setUp(self):
        # Generic Tournament config
        self.name = "Tetris 1vs1"
        self.description = "Just play Tetris, yo"
        self.game_title = GameTitle("Tetris99", "Some Description", "https://de.wikipedia.org/wiki/Tetris_99", "tetris.png")
        self.format_ = TournamentFormat.SINGLE_ELIMINATION_BO_3
        self.start_time = datetime(year=2100, month=6, day=23, hour=16, minute=30, second=0)
        self.initial_status = TournamentStatus.CLOSED

        # Generic Participants
        self.participant_a = Participant(1, ParticipantType.PLAYER)
        self.participant_b = Participant(2, ParticipantType.PLAYER)
        self.participant_c = Participant(3, ParticipantType.PLAYER)

    def test_tournament_without_participants_can_not_be_started(self) -> None:
        tournament_under_test = generate_new_tournament(self.name, self.description, self.game_title, self.format_, self.start_time, 32, self.initial_status)
        with self.assertRaises(TournamentError):
            tournament_under_test.start()

    def test_adding_the_same_participant_twice_leads_to_exception(self) -> None:
        tournament_under_test = generate_new_tournament(self.name, self.description, self.game_title, self.format_, self.start_time, 32, self.initial_status)
        tournament_under_test.add_participant(self.participant_a)
        with self.assertRaises(TournamentError):
            tournament_under_test.add_participant(self.participant_a)

    def test_single_elimination_bo3_tournament_gets_generated_correctly(self) -> None:
        tournament_under_test = generate_new_tournament(self.name, self.description, self.game_title, self.format_, self.start_time, 32, self.initial_status)

        tournament_under_test.add_participant(self.participant_a)
        tournament_under_test.add_participant(self.participant_b)
        tournament_under_test.add_participant(self.participant_c)
        tournament_under_test.start()

        # Assert Tournament was switched to ONGOING
        self.assertEqual(TournamentStatus.ONGOING, tournament_under_test.status)

        matches_in_tournament = sorted(tournament_under_test.matches, key=lambda m: m.match_id)

        # First match
        fm = matches_in_tournament[0]
        self.assertEqual(fm.status, MatchStatus.PENDING)
        self.assertEqual(fm.participants[0].participant_id, self.participant_a.id)
        self.assertEqual(fm.participants[0].slot_number, 1)
        self.assertEqual(fm.participants[1].participant_id, self.participant_b.id)
        self.assertEqual(fm.participants[1].slot_number, 2)

        # Second match (Bye)
        sm = matches_in_tournament[1]
        self.assertEqual(sm.status, MatchStatus.COMPLETED)
        self.assertEqual(sm.participants[0].participant_id, self.participant_c.id)
        self.assertEqual(sm.participants[0].slot_number, 1)
        self.assertEqual(sm.participants[0].participant_id, sm.winner)

        # Third match (Final)
        sm = matches_in_tournament[2]
        self.assertEqual(sm.status, MatchStatus.WAITING)
        self.assertEqual(sm.participants[0].participant_id, self.participant_c.id)
        self.assertEqual(sm.participants[0].slot_number, 1)
        self.assertIsNone(sm.winner)

    def test_ffa_tournament_with_15_participants_gets_generated_correctly(self) -> None:
        tournament_under_test = generate_new_tournament("Among Us", "It's Among Us", GameTitle("Among Us", "", "", ""), TournamentFormat.FFA, self.start_time, 32, TournamentStatus.OPEN)

        for i in range(1, 16):
            tournament_under_test.add_participant(Participant(i, ParticipantType.PLAYER))
        tournament_under_test.start()

        # Assert Tournament was switched to ONGOING
        self.assertEqual(TournamentStatus.ONGOING, tournament_under_test.status)

        matches_in_tournament = sorted(tournament_under_test.matches, key=lambda m: m.match_id)

        self.assertEqual(1, len(matches_in_tournament))
        self.assertEqual(15, len(matches_in_tournament[0].participants))
