from Database.database import QuestionDataBase
import random



class Game:
    def __init__(self, teamsize=0):
        self.teamsize = teamsize
        self.scoreboard = {'score_red': 0,
                           'score_blue': 0,
                           'score_green': 0}

        self.questions = QuestionDataBase('src_folder/BackEnd/Database/EinsZweiOderDrei.db')
        self.available_questions = list(range(1, self.questions.num_rows()))

    def set_teamsize(self, teamsize: int):
        self.teamsize = teamsize
    
    def add_score(self, current_score: dict):
        for key in self.scoreboard.keys():
            if key in current_score.keys():
                self.scoreboard[key] = self.scoreboard[key] + current_score[key]
        return self.scoreboard
    
    def set_scoreboard(self, current_scores: dict):
        self.scoreboard = self.add_score(current_scores)
        return self.scoreboard

    def reset_game(self):
        self.__init__()

    def get_question(self) -> dict:
        questions = self.questions.cursor.execute("Select * from Questions")
        field_names = [i[0] for i in questions.description]

        questions_data = questions.fetchall()
        random_question_number = random.choice(self.available_questions)
        self.available_questions.remove(random_question_number)

        question= list(questions_data[random_question_number])
        data = dict(zip(field_names, question))
        add_data = my_game.correct_field(data)
        return add_data
    
    def correct_field(self, data_dict: dict) -> dict:
        answeroption = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3', 'Correct_answeroption']
        for i in range(0, len(answeroption) -1):
            if data_dict[answeroption[-1]] == data_dict[answeroption[i]]:
                index = answeroption.index(answeroption[i]) + 1
                break
        data_dict.update([('Correct_field', index)])
        return data_dict
    


my_game = Game()
test = my_game.get_question()


