# -*- coding: utf-8 -*-
"""
Created on Thu Nov  6 22:30:12 2025

@author: mstep
"""
import tkinter as tk
from PIL import Image, ImageTk, ImageDraw

class CloseupMapApp:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title('closeup_map')
        self.window.protocol('WM_DELETE_WINDOW', self.kill_callback)

        # Make the canvas.
        self.canvas = tk.Canvas(self.window, bg='white',
            borderwidth=0, highlightthickness=0, cursor='crosshair')
        self.canvas.pack(padx=10, pady=10,
            side=tk.TOP, fill=tk.BOTH, expand=True)

        # Load the full-scale map.
        map_name = 'usmap.gif'
        self.map_image = Image.open(map_name).convert('RGBA')

        # Load the small-scale map.
        # (I tried resizing the large map, but a lof of state borders
        # and other thin features came out looking dashed.)
        small_map_name = 'usmapsmall.jpg'
        self.small_map_image = Image.open(small_map_name).convert('RGBA')

        # Display the small-scale map.
        self.small_map_photo_image = ImageTk.PhotoImage(self.small_map_image)
        self.canvas.create_image(0, 0, image=self.small_map_photo_image,
                                 anchor='nw')
        wid = self.small_map_image.width
        hgt = self.small_map_image.height
        self.canvas.config(width=wid, height=hgt)

        # Radius of the closeup area in small and large scales.
        self.radius = 50
        self.scale = self.map_image.width / self.small_map_image.width

        # Track mouse motion over the canvas.
        self.canvas.bind('<Motion>', self.mouse_move)

        # Display the window.
        self.window.focus_force()
        self.window.mainloop()

    def kill_callback(self):
        # Destroy the tkinter window.
        self.window.destroy()

    def mouse_move(self, event):
        '''Display the closeup.'''
        self.show_closeup(event.x, event.y)

    def show_closeup(self, x, y):
        '''Display the closeup centered at (x, y).'''
        # Make a mask image.
        wid = self.map_image.width
        hgt = self.map_image.height
        mask = Image.new('L', (wid, hgt), 0)

        # Figure out where to draw the big map to position the closeup.
        big_x = self.scale * x
        big_y = self.scale * y

        # Make a white circle for the closeup.
        dr = ImageDraw.Draw(mask)
        bounds = (big_x - self.radius, big_y - self.radius,
                  big_x + self.radius, big_y + self.radius)
        dr.ellipse(bounds, fill='white', outline='white')
        # mask.save('mask.png')

        # Copy the small map image so we don't mess up the original.
        map_copy = self.small_map_image.copy()

        # Draw the big map onto the copy of the little one using the mask.
        paste_x = int(x - big_x)
        paste_y = int(y - big_y)
        map_copy.paste(self.map_image, box=(paste_x, paste_y), mask=mask)

        # Draw a circle around the area.
        dr = ImageDraw.Draw(map_copy)
        rect = (x-self.radius, y-self.radius, x+self.radius, y+self.radius)
        dr.ellipse(rect, outline='blue', width=3)
        # map_copy.save('copy.png')

        # Display the result.
        self.small_map_photo_image = ImageTk.PhotoImage(map_copy)
        self.canvas.delete(tk.ALL)
        self.canvas.create_image(0, 0, image=self.small_map_photo_image,
                                 anchor='nw')

#%%
CloseupMapApp()
