import tkinter as tk
from tkinter import ttk
import random
import math

class AfricaGeographyQuiz:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("African Geography Quiz")
        self.root.geometry("800x700")
        self.root.configure(bg='lightblue')
        
        # African countries and their approximate center coordinates (normalized 0-1)
        self.countries = {
            'Algeria': (0.5, 0.25),
            'Angola': (0.35, 0.75),
            'Benin': (0.45, 0.55),
            'Botswana': (0.55, 0.8),
            'Burkina Faso': (0.4, 0.5),
            'Burundi': (0.65, 0.7),
            'Cameroon': (0.5, 0.6),
            'Cape Verde': (0.15, 0.45),
            'Central African Republic': (0.55, 0.6),
            'Chad': (0.55, 0.5),
            'Comoros': (0.8, 0.75),
            'Democratic Republic of the Congo': (0.55, 0.7),
            'Republic of the Congo': (0.5, 0.65),
            'Djibouti': (0.8, 0.5),
            'Egypt': (0.65, 0.25),
            'Equatorial Guinea': (0.48, 0.6),
            'Eritrea': (0.75, 0.45),
            'Eswatini': (0.65, 0.85),
            'Ethiopia': (0.75, 0.55),
            'Gabon': (0.48, 0.65),
            'Gambia': (0.25, 0.5),
            'Ghana': (0.4, 0.55),
            'Guinea': (0.3, 0.5),
            'Guinea-Bissau': (0.25, 0.5),
            'Ivory Coast': (0.35, 0.55),
            'Kenya': (0.75, 0.65),
            'Lesotho': (0.6, 0.85),
            'Liberia': (0.3, 0.55),
            'Libya': (0.55, 0.3),
            'Madagascar': (0.85, 0.8),
            'Malawi': (0.7, 0.75),
            'Mali': (0.4, 0.45),
            'Mauritania': (0.3, 0.4),
            'Mauritius': (0.9, 0.8),
            'Morocco': (0.35, 0.2),
            'Mozambique': (0.7, 0.8),
            'Namibia': (0.5, 0.8),
            'Niger': (0.5, 0.45),
            'Nigeria': (0.5, 0.55),
            'Rwanda': (0.65, 0.7),
            'Sao Tome and Principe': (0.45, 0.6),
            'Senegal': (0.25, 0.45),
            'Seychelles': (0.9, 0.7),
            'Sierra Leone': (0.28, 0.55),
            'Somalia': (0.85, 0.6),
            'South Africa': (0.6, 0.85),
            'South Sudan': (0.65, 0.55),
            'Sudan': (0.65, 0.45),
            'Tanzania': (0.7, 0.7),
            'Togo': (0.43, 0.55),
            'Tunisia': (0.5, 0.2),
            'Uganda': (0.7, 0.65),
            'Zambia': (0.6, 0.75),
            'Zimbabwe': (0.65, 0.8)
        }
        
        self.current_country = None
        self.showing_answer = False
        
        self.setup_ui()
        self.next_question()
        
    def setup_ui(self):
        # Title
        title_label = tk.Label(self.root, text="African Geography Quiz", 
                              font=('Arial', 20, 'bold'), bg='lightblue', fg='darkblue')
        title_label.pack(pady=10)
        
        # Instructions
        instructions = tk.Label(self.root, 
                               text="Click on the map where you think the country is located!", 
                               font=('Arial', 12), bg='lightblue', fg='darkgreen')
        instructions.pack(pady=5)
        
        # Country prompt
        self.country_label = tk.Label(self.root, text="", 
                                     font=('Arial', 16, 'bold'), bg='lightblue', fg='red')
        self.country_label.pack(pady=10)
        
        # Canvas for the map
        self.canvas = tk.Canvas(self.root, width=600, height=500, bg='lightcyan')
        self.canvas.pack(pady=10)
        self.canvas.bind("<Button-1>", self.on_map_click)
        
        # Draw Africa outline
        self.draw_africa_outline()
        
    def draw_africa_outline(self):
        """Draw a simplified outline of Africa"""
        # Simplified Africa outline coordinates (scaled to canvas)
        africa_points = [
            # Starting from northwest, going clockwise
            100, 150,  # Morocco area
            120, 140,  # North Morocco
            140, 130,  # Algeria
            200, 120,  # Tunisia
            250, 130,  # Libya
            350, 140,  # Egypt
            370, 160,  # Egypt east
            380, 200,  # Red Sea
            390, 250,  # Sudan/Eritrea
            400, 300,  # Ethiopia
            420, 350,  # Somalia
            430, 400,  # Kenya
            440, 450,  # Tanzania
            450, 480,  # Mozambique north
            460, 500,  # Mozambique
            450, 520,  # South Africa east
            400, 530,  # South Africa
            350, 525,  # Lesotho area
            300, 520,  # South Africa west
            250, 510,  # Namibia
            200, 500,  # Angola
            150, 480,  # Angola north
            120, 450,  # DRC
            100, 400,  # Cameroon
            90, 350,   # Nigeria
            85, 300,   # Niger
            80, 250,   # Mali
            75, 200,   # Mauritania
            80, 180,   # Morocco west
            100, 150   # Back to start
        ]
        
        self.canvas.create_polygon(africa_points, outline='black', fill='tan', width=2)
        
    def next_question(self):
        """Select a random country for the next question"""
        if self.showing_answer:
            return
            
        self.current_country = random.choice(list(self.countries.keys()))
        self.country_label.config(text=f"Find: {self.current_country}")
        
        # Clear any previous answer markers
        self.canvas.delete("answer_marker")
        
    def on_map_click(self, event):
        """Handle clicks on the map"""
        if self.showing_answer:
            return
            
        # Show where the user clicked
        self.canvas.create_oval(event.x-5, event.y-5, event.x+5, event.y+5, 
                               fill='blue', outline='darkblue', width=2, tags="click_marker")
        
        # Show the correct location
        self.show_answer()
        
    def show_answer(self):
        """Show the correct location of the country"""
        self.showing_answer = True
        
        # Get the country's coordinates and convert to canvas coordinates
        country_coords = self.countries[self.current_country]
        canvas_x = country_coords[0] * 600  # Scale to canvas width
        canvas_y = country_coords[1] * 500  # Scale to canvas height
        
        # Adjust coordinates to fit within Africa outline
        canvas_x = max(75, min(525, canvas_x))
        canvas_y = max(120, min(480, canvas_y))
        
        # Draw the correct location marker
        self.canvas.create_oval(canvas_x-8, canvas_y-8, canvas_x+8, canvas_y+8,
                               fill='red', outline='darkred', width=3, tags="answer_marker")
        
        # Draw country name near the marker
        self.canvas.create_text(canvas_x, canvas_y-20, text=self.current_country,
                               font=('Arial', 10, 'bold'), fill='darkred', tags="answer_marker")
        
        # Schedule next question
        self.root.after(3000, self.prepare_next_question)
        
    def prepare_next_question(self):
        """Prepare for the next question"""
        self.showing_answer = False
        # Clear click markers
        self.canvas.delete("click_marker")
        # Answer markers will be cleared in next_question
        self.next_question()
        
    def run(self):
        """Start the application"""
        self.root.mainloop()

if __name__ == "__main__":
    app = AfricaGeographyQuiz()
    app.run()