from tkinter import *
from quiz_brain import QuizBrain
THEME_COLOR = "#375362"

class QuizInterface:
    
    def __init__(self, quiz_brain: QuizBrain) -> None:
        self.brain = quiz_brain
        self.window = Tk()
        self.window.title("Quizzler")
        self.window.config(padx=20, pady=20, bg=THEME_COLOR)
        
        self.score_label = Label(text="Score : 0", bg=THEME_COLOR, fg="white")
        self.score_label.grid(row=0, column=1)
        
        self.canvas = Canvas(self.window, width=300, height=250, bg="white")
        self.question_text = self.canvas.create_text(150, 125,text="", width=280, fill=THEME_COLOR, font=("Helvetica", 20, "italic"))
        self.canvas.grid(row=1, column=0, columnspan=2, pady=50)
        
        true_image = PhotoImage(file="images/true.png")
        self.true_button = Button(self.window, image=true_image, bg=THEME_COLOR, highlightthickness=0, command=self.true_pressed)
        self.true_button.grid(row=2, column=0)
        
        false_image = PhotoImage(file="images/false.png")
        self.false_button = Button(self.window, image=false_image, bg=THEME_COLOR, highlightthickness=0, command=self.false_pressed)
        self.false_button.grid(row=2, column=1)
        
        self.get_next_question()
        
        self.window.mainloop()

    def get_next_question(self):
        self.canvas.config(bg="white")
        if self.brain.still_has_questions():
            self.score_label.config(text="Score : {}".format(self.brain.score))
            question_text = self.brain.next_question()
            self.canvas.itemconfig(self.question_text, text=question_text)
        else:
            self.canvas.itemconfig(self.question_text, text="You have finished the quiz!\nYour score is {}".format(self.brain.score))
            self.true_button.config(state="disabled")
            self.false_button.config(state="disabled")
    
    def true_pressed(self):
        self.give_feedback(self.brain.check_answer("True"))
    
    def false_pressed(self):
        self.give_feedback(self.brain.check_answer("False"))

    def give_feedback(self, is_right):
        if is_right:
            self.canvas.config(bg="green")
        else:
            self.canvas.config(bg="red")
        
        self.window.after(1000, self.get_next_question)