import tkinter as tk
from time import strftime
import math
from datetime import datetime
import colorsys

class CoolClock:
    def __init__(self, root):
        self.root = root
        self.root.title("✨ 酷炫时钟 ✨")
        self.root.configure(bg='black')
        self.root.attributes('-alpha', 0.95)  # 透明度效果
        
        # 设置窗口大小和位置居中
        window_width = 800
        window_height = 700
        screen_width = root.winfo_screenwidth()
        screen_height = root.winfo_screenheight()
        center_x = int(screen_width/2 - window_width/2)
        center_y = int(screen_height/2 - window_height/2)
        root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
        
        # 创建主画布
        self.canvas = tk.Canvas(root, width=window_width, height=window_height, 
                               bg='black', highlightthickness=0)
        self.canvas.pack(fill=tk.BOTH, expand=True)
        
        # 时钟参数
        self.center_x = window_width // 2
        self.center_y = window_height // 2
        self.clock_radius = 200
        self.colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7']
        self.current_color_index = 0
        self.particles = []
        
        # 创建标签显示数字时间
        self.time_label = tk.Label(root, font=('DS-Digital', 40, 'bold'),
                                  fg='white', bg='black')
        self.time_label.place(relx=0.5, rely=0.9, anchor=tk.CENTER)
        
        # 创建日期标签
        self.date_label = tk.Label(root, font=('微软雅黑', 16),
                                 fg='#4ECDC4', bg='black')
        self.date_label.place(relx=0.5, rely=0.85, anchor=tk.CENTER)
        
        # 创建控制按钮
        self.create_buttons()
        
        # 开始动画
        self.update_clock()
        self.animate_background()
        
    def create_buttons(self):
        """创建控制按钮"""
        button_frame = tk.Frame(self.root, bg='black')
        button_frame.place(relx=0.5, rely=0.05, anchor=tk.CENTER)
        
        tk.Button(button_frame, text="切换颜色", command=self.change_colors,
                 bg='#333', fg='white', font=('微软雅黑', 10),
                 relief='flat', padx=10, pady=5).pack(side=tk.LEFT, padx=5)
        
        tk.Button(button_frame, text="添加粒子", command=self.add_particles,
                 bg='#333', fg='white', font=('微软雅黑', 10),
                 relief='flat', padx=10, pady=5).pack(side=tk.LEFT, padx=5)
        
        tk.Button(button_frame, text="全屏/窗口", command=self.toggle_fullscreen,
                 bg='#333', fg='white', font=('微软雅黑', 10),
                 relief='flat', padx=10, pady=5).pack(side=tk.LEFT, padx=5)
        
    def draw_clock_face(self):
        """绘制表盘"""
        self.canvas.delete("clock_face")
        
        # 绘制外圆（发光效果）
        for i in range(5, 0, -1):
            self.canvas.create_oval(
                self.center_x - self.clock_radius - i,
                self.center_y - self.clock_radius - i,
                self.center_x + self.clock_radius + i,
                self.center_y + self.clock_radius + i,
                outline=self.colors[self.current_color_index],
                width=1, tags="clock_face"
            )
        
        # 绘制主要表盘
        self.canvas.create_oval(
            self.center_x - self.clock_radius,
            self.center_y - self.clock_radius,
            self.center_x + self.clock_radius,
            self.center_y + self.clock_radius,
            outline=self.colors[self.current_color_index],
            width=4, tags="clock_face"
        )
        
        # 绘制刻度
        for i in range(60):
            angle = math.radians(6 * i)
            if i % 5 == 0:  # 小时刻度
                length = 20
                width = 3
                # 添加小时数字
                if i % 5 == 0:
                    hour = i // 5
                    if hour == 0:
                        hour = 12
                    text_radius = self.clock_radius - 35
                    x = self.center_x + text_radius * math.sin(angle)
                    y = self.center_y - text_radius * math.cos(angle)
                    self.canvas.create_text(x, y, text=str(hour),
                                          font=('Arial', 16, 'bold'),
                                          fill='white', tags="clock_face")
            else:  # 分钟刻度
                length = 10
                width = 1
            
            x1 = self.center_x + (self.clock_radius - 10) * math.sin(angle)
            y1 = self.center_y - (self.clock_radius - 10) * math.cos(angle)
            x2 = self.center_x + (self.clock_radius - 10 - length) * math.sin(angle)
            y2 = self.center_y - (self.clock_radius - 10 - length) * math.cos(angle)
            
            self.canvas.create_line(x1, y1, x2, y2,
                                  fill=self.colors[self.current_color_index],
                                  width=width, tags="clock_face")
    
    def draw_hands(self):
        """绘制指针"""
        now = datetime.now()
        hours = now.hour % 12
        minutes = now.minute
        seconds = now.second
        
        # 删除旧的指针
        self.canvas.delete("hands")
        
        # 计算角度
        hour_angle = math.radians((hours + minutes/60) * 30 - 90)
        minute_angle = math.radians(minutes * 6 - 90)
        second_angle = math.radians(seconds * 6 - 90)
        
        # 绘制时针
        hour_x = self.center_x + (self.clock_radius * 0.5) * math.cos(hour_angle)
        hour_y = self.center_y + (self.clock_radius * 0.5) * math.sin(hour_angle)
        self.canvas.create_line(self.center_x, self.center_y, hour_x, hour_y,
                              width=8, fill='white', 
                              arrow='last', arrowshape=(20, 25, 8),
                              tags="hands")
        
        # 绘制分针
        minute_x = self.center_x + (self.clock_radius * 0.7) * math.cos(minute_angle)
        minute_y = self.center_y + (self.clock_radius * 0.7) * math.sin(minute_angle)
        self.canvas.create_line(self.center_x, self.center_y, minute_x, minute_y,
                              width=6, fill='#4ECDC4',
                              arrow='last', arrowshape=(15, 20, 6),
                              tags="hands")
        
        # 绘制秒针
        second_x = self.center_x + (self.clock_radius * 0.8) * math.cos(second_angle)
        second_y = self.center_y + (self.clock_radius * 0.8) * math.sin(second_angle)
        self.canvas.create_line(self.center_x, self.center_y, second_x, second_y,
                              width=2, fill='#FF6B6B',
                              arrow='last', arrowshape=(10, 15, 4),
                              tags="hands")
        
        # 绘制中心圆点
        self.canvas.create_oval(self.center_x-8, self.center_y-8,
                              self.center_x+8, self.center_y+8,
                              fill='white', outline='white',
                              tags="hands")
    
    def update_digital_clock(self):
        """更新数字时钟和日期"""
        now = datetime.now()
        
        # 时间显示
        time_str = now.strftime('%H:%M:%S')
        self.time_label.config(text=time_str)
        
        # 日期显示
        date_str = now.strftime('%Y年%m月%d日 星期')
        weekdays = ['一', '二', '三', '四', '五', '六', '日']
        weekday = weekdays[now.weekday()]
        date_str += weekday
        
        # 添加农历年份（示例：丙午年）
        lunar_year = "丙午年"
        date_str += f"  {lunar_year}"
        
        self.date_label.config(text=date_str)
    
    def add_particles(self):
        """添加粒子效果"""
        for _ in range(20):
            angle = math.radians(360 * random.random())
            radius = random.randint(10, 30)
            speed = random.uniform(0.5, 2.0)
            particle = {
                'x': self.center_x + random.randint(-50, 50),
                'y': self.center_y + random.randint(-50, 50),
                'radius': radius,
                'speed': speed,
                'color': random.choice(self.colors),
                'direction': angle
            }
            self.particles.append(particle)
    
    def update_particles(self):
        """更新粒子位置"""
        for particle in self.particles[:]:
            particle['x'] += particle['speed'] * math.cos(particle['direction'])
            particle['y'] += particle['speed'] * math.sin(particle['direction'])
            
            # 如果粒子飞出屏幕，移除它
            if (particle['x'] < 0 or particle['x'] > 800 or 
                particle['y'] < 0 or particle['y'] > 700):
                self.particles.remove(particle)
    
    def draw_particles(self):
        """绘制所有粒子"""
        self.canvas.delete("particles")
        for particle in self.particles:
            x, y = particle['x'], particle['y']
            radius = particle['radius']
            self.canvas.create_oval(x-radius, y-radius, x+radius, y+radius,
                                  fill=particle['color'], outline='',
                                  tags="particles")
    
    def animate_background(self):
        """背景动画效果"""
        hue = (datetime.now().second / 60.0)
        r, g, b = colorsys.hsv_to_rgb(hue, 0.2, 0.1)  # 低饱和度，低亮度
        color = f'#{int(r*255):02x}{int(g*255):02x}{int(b*255):02x}'
        self.root.configure(bg=color)
        self.time_label.config(bg=color)
        self.date_label.config(bg=color)
        
        self.root.after(100, self.animate_background)
    
    def change_colors(self):
        """切换颜色主题"""
        self.current_color_index = (self.current_color_index + 1) % len(self.colors)
    
    def toggle_fullscreen(self):
        """切换全屏模式"""
        self.root.attributes('-fullscreen', not self.root.attributes('-fullscreen'))
    
    def update_clock(self):
        """更新时钟"""
        # 清除画布
        self.canvas.delete("all")
        
        # 绘制表盘
        self.draw_clock_face()
        
        # 绘制指针
        self.draw_hands()
        
        # 更新数字时间
        self.update_digital_clock()
        
        # 更新粒子
        self.update_particles()
        self.draw_particles()
        
        # 添加秒针轨迹效果
        now = datetime.now()
        for i in range(1, 6):
            second_angle = math.radians(((now.second - i) % 60) * 6 - 90)
            x = self.center_x + (self.clock_radius * 0.8) * math.cos(second_angle)
            y = self.center_y + (self.clock_radius * 0.8) * math.sin(second_angle)
            radius = 3 - i * 0.5
            alpha = 0.7 - i * 0.1
            self.canvas.create_oval(x-radius, y-radius, x+radius, y+radius,
                                  fill='#FF6B6B', outline='',
                                  tags="trail")
        
        # 100毫秒后再次更新
        self.root.after(100, self.update_clock)

# 运行时钟
if __name__ == "__main__":
    import random
    
    root = tk.Tk()
    clock = CoolClock(root)
    
    # 绑定退出快捷键
    root.bind('<Escape>', lambda e: root.quit())
    root.bind('<space>', lambda e: clock.add_particles())
    
    root.mainloop()