import tkinter as tk
from tkinter import messagebox, simpledialog
import json
from datetime import datetime

DATA_FILE = "birthdays.json"


class BirthdayReminder:
    def __init__(self, root):
        self.root = root
        self.root.title("🎂 特殊生日提醒器")
        self.root.geometry("400x400")

        self.birthdays = self.load_data()

        # 标题
        tk.Label(root, text="🎉 生日提醒器", font=("微软雅黑", 16)).pack(pady=10)

        # 列表框
        self.listbox = tk.Listbox(root, width=40, font=("微软雅黑", 12))
        self.listbox.pack(pady=10)
        self.refresh_list()

        # 按钮
        tk.Button(root, text="➕ 添加生日", command=self.add_birthday).pack(pady=5)
        tk.Button(root, text="🔍 检查今日生日", command=self.check_today).pack(pady=5)

        # 启动时自动检查
        self.check_today()

    def load_data(self):
        try:
            with open(DATA_FILE, "r", encoding="utf-8") as f:
                return json.load(f)
        except FileNotFoundError:
            return {}

    def save_data(self):
        with open(DATA_FILE, "w", encoding="utf-8") as f:
            json.dump(self.birthdays, f, ensure_ascii=False, indent=2)

    def refresh_list(self):
        self.listbox.delete(0, tk.END)
        for name, date in self.birthdays.items():
            self.listbox.insert(tk.END, f"{name}：{date}")

    def add_birthday(self):
        name = simpledialog.askstring("姓名", "请输入姓名：")
        if not name:
            return

        date = simpledialog.askstring("生日", "请输入生日（YYYY-MM-DD）：")
        try:
            datetime.strptime(date, "%Y-%m-%d")
        except ValueError:
            messagebox.showerror("错误", "日期格式不正确！")
            return

        self.birthdays[name] = date
        self.save_data()
        self.refresh_list()
        messagebox.showinfo("成功", f"已添加 {name} 的生日 🎂")

    def check_today(self):
        today = datetime.now().strftime("%m-%d")
        found = []

        for name, date in self.birthdays.items():
            if date[5:] == today:
                found.append(name)

        if found:
            messagebox.showinfo(
                "🎉 生日提醒",
                "\n".join(f"今天是 {name} 的生日！🎂" for name in found)
            )
        else:
            messagebox.showinfo("📅 提醒", "今天没有人生日~")


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