import tkinter as tk
import random
import time
import threading

# Very simplified Africa outline coordinates (replace with better outline if desired):
AFRICA_OUTLINE = [
    (50, 20), (100, 10), (160, 15), (200, 40), (240, 80),
    (250, 130), (230, 180), (210, 220), (170, 260), (120, 280),
    (80, 300), (60, 260), (50, 200), (40, 150), (40, 80)
]

# Dictionary mapping country names to rough polygon coordinates.
# (These are approximate positions and sizes – refine or expand as needed.)
COUNTRY_COORDS = {
    "Egypt": [(160, 40), (200, 45), (200, 75), (160, 70)],
    "Nigeria": [(110, 145), (140, 145), (140, 175), (110, 175)],
    "South Africa": [(90, 230), (130, 230), (130, 260), (90, 260)],
    "Kenya": [(165, 130), (190, 130), (190, 160), (165, 160)],
    "Morocco": [(75, 50), (100, 50), (100, 80), (75, 80)],
    "Madagascar": [(245, 200), (265, 200), (265, 250), (245, 250)]
}

COUNTRY_LIST = list(COUNTRY_COORDS.keys())


class AfricaQuizApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Africa Geography Quiz")

        self.canvas = tk.Canvas(root, width=300, height=320)
        self.canvas.pack()

        # Draw Africa outline
        self.canvas.create_polygon(AFRICA_OUTLINE, outline="black", fill="white")

        self.label = tk.Label(root, text="", font=("Arial", 14))
        self.label.pack(pady=8)

        self.current_country = None
        self.country_outline = None

        self.canvas.bind("<Button-1>", self.on_click)

        self.next_country()

    def on_click(self, event):
        # Draw country outline regardless of whether click was "correct"
        if self.country_outline is not None:
            self.canvas.delete(self.country_outline)

        coords = COUNTRY_COORDS[self.current_country]
        self.country_outline = self.canvas.create_polygon(
            coords, outline="red", fill="", width=2
        )
        # Start a thread so the UI does not freeze
        threading.Thread(target=self.show_then_next).start()

    def show_then_next(self):
        time.sleep(2.0)  # show the outline for two seconds
        self.canvas.delete(self.country_outline)
        self.country_outline = None
        self.next_country()

    def next_country(self):
        self.current_country = random.choice(COUNTRY_LIST)
        self.label.config(text=f"Click: {self.current_country}")


if __name__ == "__main__":
    root = tk.Tk()
    app = AfricaQuizApp(root)
    root.mainloop()
