# -*- coding: utf-8 -*-
"""
Created on Sat Aug 16 15:08:38 2025

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

class ClockApp:
    # Create and manage the tkinter interface.
    def __init__(self):
        # Make the main interface.
        self.window = tk.Tk()
        self.window.title('')
        self.window.protocol('WM_DELETE_WINDOW', self.kill_callback)

        self.window.wm_attributes('-transparentcolor', 'pink')
        self.window.wm_attributes('-topmost', True)

        method = 1
        # Remove all window decorations. Cannot minimize, maximize, resize,
        # or move. Doesn't appear in taskbar. Kill with Alt+F4.
        if method == 1:
            self.window.overrideredirect(True)
            self.window.configure(borderwidth=0, highlightthickness=0) 
            #self.window.attributes("-alpha", 0.6) # Translucent

        # In Windows, tool window style. Cannot minimize or maximize. Can
        # resize. Doesn't appear in taskbar. Kill with Alt+F4 or click X.
        if method == 2:
            self.window.wm_attributes('-toolwindow', 'True')

        # Prevent ressizing. Can minimize and restore. Appears in taskbar.
        # Kill with Alt+F4 or click X.
        if method == 3:
            self.window.resizable(False, False)

        # Set drawing parameters.
        self.clock_wid = 200
        self.radius = self.clock_wid / 2
        self.second_radius = self.radius * 0.9
        self.minute_radius = self.radius * 0.6
        self.hour_radius = self.radius * 0.3
        self.cx = self.radius
        self.cy = self.radius

        # Make the clock widgets.
        self.make_clock_face()

        # Move the window with mouse events.
        self.window.bind("<Button-1>", self.mouse_down)
        self.window.bind("<B1-Motion>", self.mouse_move)

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

    def mouse_down(self, event):
        '''Begin moving the window.'''
        self.window.start_x = event.x
        self.window.start_y = event.y

    def mouse_move(self, event):
        '''Continue moving the window.'''
        x = self.window.winfo_x() + event.x - self.window.start_x
        y = self.window.winfo_y() + event.y - self.window.start_y
        self.window.geometry(f"+{x}+{y}")

    def make_clock_face(self):
        # Make the PIL image to hold the face.
        size = (self.clock_wid, self.clock_wid)
        clock_face = Image.new('RGBA', size, color='pink')

        dr = ImageDraw.Draw(clock_face)
        bounds = ((0, 0), (self.clock_wid-1, self.clock_wid-1))
        dr.ellipse(bounds, fill='pink', outline='black', width=2)

        # Loop through image files to make the tick marks.
        slice_wid = self.clock_wid // 4
        slice_radius = self.clock_wid / 3
        angle = 0
        for filename in glob.glob('*.png'):
            # Skip the post's picture analog_clock_cake.png.
            if filename != 'analog_clock_cake.png':
                self.draw_slice(clock_face, filename, angle, self.cx, self.cy,
                                slice_wid, slice_radius)
                angle += 360 / 12

        # Save the clock face without clock hands.
        self.clock_face = clock_face

        # Display the clock face.
        photo_image = ImageTk.PhotoImage(clock_face)
        self.face_label = tk.Label(self.window, image=photo_image,
                               highlightthickness=0, borderwidth=0)
        self.face_label.pack()
        self.face_label.image = photo_image

        # Start the clock.
        self.window.focus_force()
        self.tick()

    def draw_slice(self, clock_face, filename, angle, cx, cy, slice_wid,
                   slice_radius):
        '''Use this file as a tick mark.'''
        # Load the image file.
        image = Image.open(filename)

        # Resize.
        slice_hgt = int(slice_wid * image.height / image.width)
        image = image.resize((slice_wid, slice_hgt))

        # Rotate.
        image = image.rotate(180-angle, expand=True, fillcolor=None)

        # Draw.
        radians = math.radians(angle)
        x0 = int(cx + math.cos(radians) * slice_radius - image.width / 2)
        y0 = int(cy + math.sin(radians) * slice_radius - image.height / 2)
        x1 = x0 + image.width
        y1 = y0 + image.height
        box = (x0, y0, x1, y1)
        clock_face.paste(image, box=box, mask=image)

    def kill_callback(self):
        self.window.destroy()

    def tick(self):
        '''Update the clock hand positions.'''
        now = datetime.now()

        # Copy the clock face.
        face = self.clock_face.copy()
        dr = ImageDraw.Draw(face)

        # Seconds.
        seconds = now.second
        second_radians = seconds / 60 * (2 * math.pi) - math.pi / 2
        x = self.cx + math.cos(second_radians) * self.second_radius
        y = self.cy + math.sin(second_radians) * self.second_radius
        dr.line(((self.cx, self.cy), (x, y)), fill='red', width=2)

        # Minutes.
        minutes = now.minute + seconds / 60
        minute_radians = minutes / 60 * (2 * math.pi) - math.pi / 2
        x = self.cx + math.cos(minute_radians) * self.minute_radius
        y = self.cy + math.sin(minute_radians) * self.minute_radius
        dr.line(((self.cx, self.cy), (x, y)), fill='red', width=4)

        # Hours.
        hours = now.hour + minutes / 60
        hour_radians = hours / 12 * (2 * math.pi) - math.pi / 2
        x = self.cx + math.cos(hour_radians) * self.hour_radius
        y = self.cy + math.sin(hour_radians) * self.hour_radius
        dr.line(((self.cx, self.cy), (x, y)), fill='red', width=6)

        # Draw the central dot.
        self.cx = self.radius
        self.cy = self.radius
        r = 5
        bounds = ((self.cx - r, self.cy - r), (self.cx + r, self.cy + r))
        dr.ellipse(bounds, fill='black')

        # Display the new image.
        photo_image = ImageTk.PhotoImage(face)
        self.face_label.configure(image=photo_image)
        self.face_label.image = photo_image

        # Try again in a second.
        DELAY = 1000            # Update every 1000 milliseconds.
        self.face_label.after(DELAY, self.tick)

#%%
ClockApp()
