from tkinter import *
from quiz_brain import QuizBrain

THEME_COLOR = "#375362"
BACKGROUND = "#ffffff"
ANSWER_WRONG = "#ff0000"
ANSWER_RIGHT = "#00ff00"
CARD_FONT = "Rubik", 20, "italic"


class QuizInterface:
    def __init__(self, quiz_brain: QuizBrain) -> None:
        self.quiz = quiz_brain
        self.window = Tk()
        self.window.title("Quizzler")
        self.window.config(padx=20, pady=20, background=THEME_COLOR)

        self.score_label = Label(
            text="Score: 0", background=THEME_COLOR, foreground=BACKGROUND)
        self.score_label.grid(row=0, column=1)

        self.canvas = Canvas(width=300, height=250, background=BACKGROUND)

        self.question_text = self.canvas.create_text(
            150, 125, width=280, text="Some Question Text", fill=THEME_COLOR, font=CARD_FONT)
        self.canvas.grid(row=1, column=0, columnspan=2, pady=50)

        false_image = PhotoImage(file="images/false.png")
        self.false_button = Button(
            image=false_image, highlightthickness=0, command=self.false_pressed)
        self.false_button.grid(row=2, column=0)

        true_image = PhotoImage(file="images/true.png")
        self.true_button = Button(
            image=true_image, highlightthickness=0, command=self.true_pressed)
        self.true_button.grid(row=2, column=1)

        self.get_next_question()

        self.window.mainloop()

    def get_next_question(self):
        self.canvas.config(background=BACKGROUND)
        if self.quiz.still_has_questions():
            self.score_label.config(text=f"Score: {self.quiz.score}")
            self.canvas.itemconfig(
                self.question_text, text=self.quiz.next_question())
        else:
            self.canvas.itemconfig(
                self.question_text, text=f"You've reached the end of the quiz")
            self.true_button.config(state="disabled")
            self.false_button.config(state="disabled")

    def true_pressed(self):
        self.give_feedback(self.quiz.check_answer("True"))

    def false_pressed(self):
        self.give_feedback(self.quiz.check_answer("False"))

    def give_feedback(self, is_right):
        if (is_right):
            self.canvas.config(background=ANSWER_RIGHT)
        else:
            self.canvas.config(background=ANSWER_WRONG)
        self.window.after(1000, self.get_next_question)
