import tkinter as tk
import random
import math

# ========================
# 配置
# ========================
WIDTH, HEIGHT = 800, 500
current_weather = "☀️ 晴天"
show_labels = True

root = tk.Tk()
root.title("🌈 天气插画生成器")
root.resizable(False, False)

canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, highlightthickness=0)
canvas.pack()

panel = tk.Frame(root)
panel.pack(fill="x")

label_info = tk.Label(panel, font=("Comic Sans MS", 11))
label_info.pack(side="left", padx=10)

# ========================
# 主题
# ========================
themes = {
    "💙 天空蓝": {"bg": "#E3F2FD", "fg": "#1565C0"},
    "🌸 樱花粉": {"bg": "#FFE4E9", "fg": "#D81B60"},
    "💚 抹茶绿": {"bg": "#E8F5E9", "fg": "#2E7D32"},
    "🧡 蜜桔橙": {"bg": "#FFF3E0", "fg": "#EF6C00"},
    "💜 梦幻紫": {"bg": "#F3E5F5", "fg": "#7B1FA2"},
    "⚫ 暗夜风": {"bg": "#212121", "fg": "#FFFFFF"},
}

def apply_theme(name):
    t = themes[name]
    root.configure(bg=t["bg"])
    panel.configure(bg=t["bg"])
    label_info.configure(bg=t["bg"], fg=t["fg"])
    draw_scene()

# ========================
# 画天空（渐变）
# ========================
def draw_sky(colors):
    for i in range(HEIGHT // 2):
        ratio = i / (HEIGHT / 2)
        r = int(colors[0][0] * (1 - ratio) + colors[1][0] * ratio)
        g = int(colors[0][1] * (1 - ratio) + colors[1][1] * ratio)
        b = int(colors[0][2] * (1 - ratio) + colors[1][2] * ratio)
        canvas.create_line(0, i, WIDTH, i, fill=f"#{r:02x}{g:02x}{b:02x}")

# ========================
# 画太阳
# ========================
def draw_sun():
    cx, cy = WIDTH - 120, 100
    # 光晕
    for r in range(50, 70, 5):
        canvas.create_oval(cx - r, cy - r, cx + r, cy + r, outline="#FFEE58", width=2)
    # 太阳本体
    canvas.create_oval(cx - 40, cy - 40, cx + 40, cy + 40, fill="#FFD600", outline="")
    # 光芒
    for i in range(12):
        angle = math.pi * 2 * i / 12
        x1 = cx + 55 * math.cos(angle)
        y1 = cy + 55 * math.sin(angle)
        x2 = cx + 75 * math.cos(angle)
        y2 = cy + 75 * math.sin(angle)
        canvas.create_line(x1, y1, x2, y2, fill="#FFD600", width=3)

# ========================
# 画云
# ========================
def draw_cloud(cx, cy, gray=False):
    color = "#CCCCCC" if gray else "#FFFFFF"
    for dx, dy, r in [(0, 0, 30), (-25, 5, 22), (25, 5, 22), (-45, 10, 16), (45, 10, 16)]:
        canvas.create_oval(cx + dx, cy + dy, cx + dx + r * 2, cy + dy + r * 2, fill=color, outline="")

# ========================
# 画地面
# ========================
def draw_ground(grass_color="#7CB342", shadow=True):
    canvas.create_rectangle(0, HEIGHT - 80, WIDTH, HEIGHT, fill=grass_color, outline="")
    if shadow:
        for i in range(0, WIDTH, 20):
            canvas.create_line(i, HEIGHT - 80, i + 15, HEIGHT - 75, fill="#558B2F", width=2)

# ========================
# 画雨
# ========================
def draw_rain():
    for _ in range(80):
        x = random.randint(0, WIDTH)
        y = random.randint(50, HEIGHT - 100)
        canvas.create_line(x, y, x - 4, y + 12, fill="#64B5F6", width=2)

# ========================
# 画闪电
# ========================
def draw_lightning():
    x = random.randint(200, WIDTH - 200)
    y = 80
    points = [x, y]
    for _ in range(6):
        x += random.randint(-20, 20)
        y += random.randint(20, 40)
        points.extend([x, y])
    canvas.create_line(*points, fill="#FFF176", width=4)
    canvas.create_line(*points, fill="#FFD600", width=2)

# ========================
# 画雪花
# ========================
def draw_snow():
    for _ in range(60):
        x = random.randint(0, WIDTH)
        y = random.randint(50, HEIGHT - 80)
        r = random.uniform(2, 4)
        canvas.create_oval(x - r, y - r, x + r, y + r, fill="#FFFFFF", outline="")
        canvas.create_line(x, y - r - 2, x, y + r + 2, fill="#BBDEFB", width=1)
        canvas.create_line(x - r - 2, y, x + r + 2, y, fill="#BBDEFB", width=1)

# ========================
# 画彩虹
# ========================
def draw_rainbow():
    colors = ["#FF0000", "#FF7F00", "#FFFF00", "#00FF00", "#0000FF", "#4B0082", "#9400D3"]
    for i, c in enumerate(colors):
        canvas.create_arc(100, 50, WIDTH - 100, 350, start=0, extent=180, style="arc", outline=c, width=12 - i)

# ========================
# 画月亮
# ========================
def draw_moon():
    cx, cy = WIDTH - 120, 100
    canvas.create_oval(cx - 35, cy - 35, cx + 35, cy + 35, fill="#FFF9C4", outline="")
    canvas.create_oval(cx - 15, cy - 20, cx + 25, cy + 20, fill="#212121", outline="")

# ========================
# 画星星
# ========================
def draw_stars():
    for _ in range(30):
        x = random.randint(20, WIDTH - 20)
        y = random.randint(20, 150)
        canvas.create_text(x, y, text="✦", font=("", 12), fill="#FFFFFF")

# ========================
# 主场景
# ========================
def draw_scene():
    canvas.delete("all")

    if current_weather == "☀️ 晴天":
        draw_sky([(135, 206, 250), (176, 224, 230)])
        draw_sun()
        draw_cloud(180, 100)
        draw_cloud(320, 130)
        draw_ground("#7CB342")

    elif current_weather == "⛅ 多云":
        draw_sky([(176, 196, 222), (220, 220, 220)])
        draw_sun()
        draw_cloud(200, 90, True)
        draw_cloud(350, 110, True)
        draw_cloud(500, 130, True)
        draw_ground("#9CCC65")

    elif current_weather == "🌧️ 雨天":
        draw_sky([(100, 100, 120), (140, 140, 160)])
        draw_cloud(200, 80, True)
        draw_cloud(400, 100, True)
        draw_rain()
        draw_ground("#689F38")

    elif current_weather == "⛈️ 雷雨":
        draw_sky([(60, 60, 80), (100, 100, 120)])
        draw_cloud(200, 80, True)
        draw_cloud(400, 100, True)
        draw_rain()
        draw_lightning()
        draw_ground("#558B2F")

    elif current_weather == "❄️ 雪天":
        draw_sky([(180, 200, 220), (200, 220, 240)])
        draw_cloud(200, 90, True)
        draw_snow()
        draw_ground("#BCAAA4")

    elif current_weather == "🌈 彩虹":
        draw_sky([(135, 206, 250), (176, 224, 230)])
        draw_sun()
        draw_rainbow()
        draw_cloud(150, 120)
        draw_ground("#7CB342")

    elif current_weather == "🌙 夜晚":
        draw_sky([(10, 20, 50), (30, 40, 80)])
        draw_moon()
        draw_stars()
        draw_ground("#33691E")

    if show_labels:
        canvas.create_text(WIDTH // 2, HEIGHT - 30, text=current_weather,
                           font=("Comic Sans MS", 16, "bold"), fill="#FFFFFF")

# ========================
# 天气切换
# ========================
weathers = ["☀️ 晴天", "⛅ 多云", "🌧️ 雨天", "⛈️ 雷雨", "❄️ 雪天", "🌈 彩虹", "🌙 夜晚"]

for w in weathers:
    tk.Button(panel, text=w, command=lambda w=w: set_weather(w)).pack(side="left", padx=2)

def set_weather(w):
    global current_weather
    current_weather = w
    draw_scene()

# ========================
# 工具按钮
# ========================
tk.Button(panel, text="🎲 随机天气", command=lambda: set_weather(random.choice(weathers))).pack(side="left", padx=5)
tk.Button(panel, text="🏷️ 标签开关", command=lambda: toggle_labels()).pack(side="left", padx=5)

def toggle_labels():
    global show_labels
    show_labels = not show_labels
    draw_scene()

for name in themes:
    tk.Button(panel, text=name, command=lambda n=name: apply_theme(n)).pack(side="right", padx=2)

# ========================
# 启动
# ========================
apply_theme("💙 天空蓝")
root.mainloop()