"""Get winner."""

import roll_dice


class GetWinner:

    @staticmethod
    def get_winner(players, first_to_start):
        """Will return the winning player."""
        first_roll = True
        i = first_to_start
        is_fast_result = False

        while i < len(players):

            # printing different text for the first roll.
            if first_roll:
                print("Player to start: " + players[i].get_name())
            else:
                print("\nPlayer turn: " + players[i].get_name())
                print("--------------------------------")
            first_roll = False

            fast_result, exit_current_game = roll_dice.Roll.roll(players, i, is_fast_result)
            # if the user wants to get fast results then we need to subtract one from i so that the turn does not
            # change.
            if fast_result:
                is_fast_result = fast_result
                i = i - 1
            # exiting the current game functionality.
            if exit_current_game:
                break

            # will return 1 if the player won.
            result = GetWinner.check_if_player_won(players, i)
            if result == 1:
                return players[i]

            i += 1

            # will reset i so that the game continues.
            if i == len(players):
                i = 0

    @staticmethod
    def check_if_player_won(players, i):
        """Will check if a player won the game."""
        if players[i].get_score() >= 100:
            for player in players:
                if player == players[i]:
                    player.add_a_game(1)
                else:
                    player.add_a_game(0)

            for player in players:
                player.score = 0

            return 1
        else:
            return 0
