import tkinter as tk
import boggle_board_randomizer as bbr
import GUI

TIME_OF_GAME = 180
WORD_FILE = 'boggle_dict.txt'


class Game:
    def __init__(self):
        """
        Initialize the game
        """
        self.__file_words = WORD_FILE
        self.__all_words = self.__read_words()
        self.__score = 0
        self.__founded_words = []

    def __read_words(self):
        """
        Read all the proper words
        :return: List of the words
        """
        with open(self.__file_words, 'r') as f:
            result = set()
            all_words = f.read().splitlines()
            for word in all_words:
                result.add(word)
        return result

    def check_and_update(self, word):
        """
        Check if a word proper, and update the score if yes
        :param word: The word to check
        :return: True if proper, False if not
        """
        if word in self.__all_words:
            if word not in self.__founded_words:
                self.__founded_words.append(word)
                self.__score += len(word)**2
                return True
            else:
                return -1
        return False

    def get_score(self):
        """
        :return: The current score
        """
        return self.__score

    def get_founded_words(self):
        """
        :return: The list of founded words
        """
        return self.__founded_words


def play_game(pause, time):
    """
    The main function, run the game
    :param pause: Boolean, if the pause is activated or not
    :param time: Int, the time of a game
    """
    play_again, exit_bool = False, False
    while not exit_bool:
        game = Game()
        game_window = tk.Tk()
        board = bbr.randomize_board()
        window = GUI.MyApp(game_window, board, game, time)
        if play_again:
            window.play(pause, time)
        else:
            window.home(True, TIME_OF_GAME)
        game_window.mainloop()
        pause = window.get_pause()
        time = window.get_time()
        exit_bool = window.get_end()
        play_again = window.get_play_again()


if __name__ == '__main__':
    pause_bool = True
    time_int = TIME_OF_GAME
    play_game(pause_bool, time_int)
