import tkinter as tk
import random

# --- High-Resolution Coordinate Data ---
# This data provides more detailed outlines for the continent and countries.
# Coordinates are in (longitude, latitude) format.
AFRICA_DATA = {
    "Algeria": [[(8.68, 28.97), (8.68, 36.8), (3.28, 36.8), (-1.9, 35.9), (-8.67, 30.9), (-8.67, 21.5), (-4.2, 25.0), (2.4, 23.4), (5.5, 19.3), (11.9, 23.9), (9.5, 28.0), (8.68, 28.97)]],
    "Angola": [[(13.2, -4.8), (11.7, -17.9), (24.0, -17.9), (24.0, -10.7), (22.0, -11.2), (21.8, -4.8), (16.5, -5.9), (13.2, -4.8)]],
    "Botswana": [[(20.0, -17.7), (20.0, -26.9), (25.3, -25.8), (29.4, -22.1), (26.7, -20.2), (20.0, -17.7)]],
    "Cameroon": [[(8.4, 2.2), (16.2, 2.2), (16.2, 13.1), (12.5, 10.5), (10.5, 11.5), (9.3, 9.5), (8.4, 4.5), (8.4, 2.2)]],
    "Chad": [[(13.4, 7.4), (24.0, 7.4), (24.0, 19.5), (20.0, 23.4), (15.0, 23.4), (14.3, 12.8), (13.4, 7.4)]],
    "Democratic Republic of the Congo": [[(12.2, -5.8), (12.2, -13.4), (29.4, -13.4), (31.2, -8.7), (29.5, -2.5), (30.8, 1.2), (30.0, 4.2), (18.5, 5.4), (16.7, 2.0), (12.2, -5.8)]],
    "Egypt": [[(25.0, 22.0), (25.0, 31.6), (34.0, 31.6), (36.9, 27.8), (34.8, 22.0), (25.0, 22.0)]],
    "Ethiopia": [[(33.0, 3.4), (44.0, 3.4), (48.0, 8.0), (43.0, 11.0), (42.2, 14.8), (36.3, 14.8), (35.0, 12.5), (33.0, 6.0), (33.0, 3.4)]],
    "Kenya": [[(33.9, -4.7), (40.9, -4.7), (41.9, -1.8), (41.0, 4.6), (34.5, 4.6), (34.0, 0.0), (33.9, -4.7)]],
    "Libya": [[(9.4, 19.5), (25.0, 19.5), (25.0, 31.6), (11.5, 33.0), (9.4, 29.0), (9.4, 19.5)]],
    "Madagascar": [[(43.2, -12.0), (50.5, -15.4), (49.5, -25.6), (43.7, -25.1), (43.2, -12.0)]],
    "Mali": [[(-12.2, 10.1), (4.2, 10.1), (4.2, 25.0), (-5.0, 25.0), (-11.5, 14.8), (-12.2, 10.1)]],
    "Morocco": [[(-13.1, 27.6), (-8.6, 27.6), (-8.6, 29.0), (-1.0, 32.0), (-1.0, 35.9), (-5.9, 35.9), (-8.8, 33.5), (-13.1, 27.6)]],
    "Mozambique": [[(30.2, -26.8), (40.8, -10.4), (35.0, -10.4), (32.5, -16.0), (30.2, -22.0), (30.2, -26.8)]],
    "Namibia": [[(11.7, -16.9), (11.7, -28.9), (20.0, -28.9), (20.0, -22.0), (25.2, -17.9), (23.0, -16.9), (11.7, -16.9)]],
    "Niger": [[(0.1, 11.9), (16.0, 11.9), (16.0, 20.2), (12.0, 23.5), (5.0, 23.5), (3.0, 16.0), (0.1, 11.9)]],
    "Nigeria": [[(2.6, 4.2), (14.6, 4.2), (14.6, 13.8), (4.0, 13.8), (2.6, 6.4), (2.6, 4.2)]],
    "Somalia": [[(41.0, -1.7), (51.4, 10.0), (51.0, 12.0), (49.0, 11.8), (41.5, 1.8), (41.0, -1.7)]],
    "South Africa": [[(16.4, -28.5), (16.4, -34.8), (28.0, -34.8), (32.9, -26.8), (31.0, -22.1), (22.0, -22.1), (20.0, -24.7), (16.4, -28.5)]],
    "Sudan": [[(21.8, 9.7), (38.6, 9.7), (38.6, 18.0), (35.0, 22.0), (25.0, 22.0), (21.8, 12.0), (21.8, 9.7)]],
    "Tanzania": [[(29.6, -11.7), (40.4, -4.5), (39.0, -1.0), (30.5, -1.0), (29.6, -8.3), (29.6, -11.7)]],
    "Zambia": [[(22.0, -18.0), (33.7, -8.2), (28.7, -8.2), (25.0, -12.0), (22.0, -16.0), (22.0, -18.0)]],
    "Zimbabwe": [[(25.2, -22.4), (33.0, -22.4), (33.0, -15.6), (27.0, -15.6), (25.2, -18.0), (25.2, -22.4)]],
}

# Outline of the African continent, including Madagascar
AFRICA_OUTLINE = [
    # Mainland
    [(-17.5, 14.8), (-16.5, 12.0), (-13.0, 7.5), (-8.0, 4.5), (1.0, 4.5), (8.5, 4.5), (9.5, 1.5), (10.0, -1.0), (12.0, -6.0), (13.0, -10.0), (15.0, -18.0), (18.4, -28.7), (20.0, -34.8), (25.0, -34.8), (32.8, -28.0), (40.8, -10.4), (44.0, -1.0), (51.4, 11.8), (45.0, 15.0), (39.6, 15.8), (34.0, 22.0), (35.8, 30.0), (25.0, 31.5), (10.0, 33.0), (10.2, 37.3), (-5.4, 34.6), (-5.9, 35.9), (-8.8, 33.5), (-13.1, 27.6), (-17.5, 20.9), (-17.5, 14.8)],
    # Madagascar
    [(43.2, -12.0), (50.5, -15.4), (49.5, -25.6), (43.7, -25.1), (43.2, -12.0)]
]

class AfricaQuizApp:
    """A simple geography quiz application for Africa."""

    def __init__(self, root):
        """Initialize the application."""
        self.root = root
        self.root.title("Africa Geography Quiz")
        self.root.configure(bg="#f0f0f0")

        # --- Configuration ---
        self.canvas_width = 600
        self.canvas_height = 650
        self.map_padding = 20
        self.country_fill_color = "#3498db" # A nice blue
        self.africa_outline_color = "#555555"
        self.africa_fill_color = "#e0e0e0"
        self.reveal_duration_ms = 2000 # 2 seconds

        # --- State Variables ---
        self.current_country = None
        self.country_polygon_ids = [] # Store multiple polygon IDs
        self.country_list = list(AFRICA_DATA.keys())

        # --- UI Elements ---
        self.info_label = tk.Label(
            self.root,
            text="Find this country:",
            font=("Helvetica", 14),
            bg="#f0f0f0"
        )
        self.info_label.pack(pady=(10, 0))

        self.country_label = tk.Label(
            self.root,
            text="",
            font=("Helvetica", 20, "bold"),
            bg="#f0f0f0",
            fg="#2c3e50"
        )
        self.country_label.pack(pady=(0, 10))

        self.canvas = tk.Canvas(
            self.root,
            width=self.canvas_width,
            height=self.canvas_height,
            bg="white",
            highlightthickness=0
        )
        self.canvas.pack(padx=20, pady=(0, 20))
        self.canvas.bind("<Button-1>", self.on_map_click)

        # --- Setup ---
        self.calculate_map_bounds()
        self.draw_africa()
        self.next_country()

    def calculate_map_bounds(self):
        """Calculate the bounding box of all coordinates to scale the map."""
        all_points = []
        for part in AFRICA_OUTLINE:
            all_points.extend(part)

        for country_polygons in AFRICA_DATA.values():
            for polygon in country_polygons:
                all_points.extend(polygon)

        longitudes = [p[0] for p in all_points]
        latitudes = [p[1] for p in all_points]

        self.min_lon = min(longitudes)
        self.max_lon = max(longitudes)
        self.min_lat = min(latitudes)
        self.max_lat = max(latitudes)

        # Calculate scale and offset to fit map in canvas
        lon_range = self.max_lon - self.min_lon
        lat_range = self.max_lat - self.min_lat

        scale_x = (self.canvas_width - 2 * self.map_padding) / lon_range
        scale_y = (self.canvas_height - 2 * self.map_padding) / lat_range
        self.scale = min(scale_x, scale_y) * 0.98 # Add a small margin

        # Calculate offsets to center the map
        map_width = lon_range * self.scale
        map_height = lat_range * self.scale
        self.x_offset = (self.canvas_width - map_width) / 2
        self.y_offset = (self.canvas_height - map_height) / 2


    def transform_coords(self, lon, lat):
        """Convert longitude and latitude to canvas coordinates."""
        x = (lon - self.min_lon) * self.scale + self.x_offset
        # Invert Y because canvas (0,0) is top-left
        y = self.canvas_height - ((lat - self.min_lat) * self.scale + self.y_offset)
        return x, y

    def draw_africa(self):
        """Draw the main outline of Africa on the canvas."""
        for part in AFRICA_OUTLINE:
            transformed_points = [self.transform_coords(lon, lat) for lon, lat in part]
            self.canvas.create_polygon(
                transformed_points,
                outline=self.africa_outline_color,
                fill=self.africa_fill_color,
                width=2
            )

    def next_country(self):
        """Selects and displays the next random country."""
        # Clear the previously shown country polygon(s) if they exist
        for polygon_id in self.country_polygon_ids:
            self.canvas.delete(polygon_id)
        self.country_polygon_ids = []

        # Pick a new random country
        self.current_country = random.choice(self.country_list)
        self.country_label.config(text=self.current_country)

    def on_map_click(self, event):
        """Handles the user clicking on the map."""
        # Prevent multiple clicks while a country is being shown
        if self.country_polygon_ids:
            return

        self.show_country_outline()
        # Schedule the next country prompt after a delay
        self.root.after(self.reveal_duration_ms, self.next_country)

    def show_country_outline(self):
        """Draws the outline of the current country."""
        if self.current_country:
            country_polygons = AFRICA_DATA[self.current_country]

            for polygon in country_polygons:
                transformed_points = [self.transform_coords(lon, lat) for lon, lat in polygon]

                # Draw each polygon part of the country and store its ID
                polygon_id = self.canvas.create_polygon(
                    transformed_points,
                    fill=self.country_fill_color,
                    outline="white",
                    width=1
                )
                self.country_polygon_ids.append(polygon_id)


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