import pygame
import sys
import calendar
from datetime import datetime

# ================== 初始化 ==================
pygame.init()
W, H = 900, 650
screen = pygame.display.set_mode((W, H), pygame.RESIZABLE)
pygame.display.set_caption("高清日历 · 计划与提醒")
clock = pygame.time.Clock()
FPS = 60

# ================== 高清配色 ==================
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY_LIGHT = (240, 242, 245)
GRAY_MID = (220, 222, 225)
GRAY_DARK = (150, 152, 155)
BLUE = (50, 130, 240)
BLUE_HOVER = (70, 150, 255)
RED = (230, 70, 70)
RED_HOVER = (250, 90, 90)
GREEN = (40, 190, 100)
PURPLE = (140, 90, 220)
BG = (250, 251, 253)

# ================== 日期 ==================
today = datetime.today()
current_year = today.year
current_month = today.month
current_day = today.day

selected_date = None
plans = {}

# ================== 字体（高清平滑） ==================
pygame.font.init()
font_title = pygame.font.SysFont("simhei", 42)
font_sub = pygame.font.SysFont("simhei", 26)
font_text = pygame.font.SysFont("simhei", 22)
font_small = pygame.font.SysFont("simhei", 18)

# ================== 圆角绘制工具 ==================
def rounded_rect(surface, color, rect, radius):
    pygame.draw.rect(surface, color, rect, border_radius=radius)

# ================== 绘制日历（高清排版） ==================
def draw_calendar(year, month):
    cal = calendar.monthcalendar(year, month)
    x_start = 60
    y_start = 140
    cell_w = 108
    cell_h = 68

    # 标题
    title = font_title.render(f"{year} 年 {month} 月", True, BLUE)
    screen.blit(title, (W//2 - title.get_width()//2, 40))

    # 星期头部
    week_names = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
    for i, w in enumerate(week_names):
        tx = x_start + i * cell_w + cell_w//2
        ty = y_start - 50
        txt = font_sub.render(w, True, GRAY_DARK)
        screen.blit(txt, (tx - txt.get_width()//2, ty))

    # 日期格子
    for r, week in enumerate(cal):
        for c, day in enumerate(week):
            x = x_start + c * cell_w
            y = y_start + r * cell_h
            rect = pygame.Rect(x, y, cell_w - 2, cell_h - 2)

            # 今天高亮
            if day == current_day and year == today.year and month == today.month:
                rounded_rect(screen, RED, rect, 10)
                txt_col = WHITE
            else:
                rounded_rect(screen, GRAY_LIGHT, rect, 10)
                txt_col = BLACK

            if day != 0:
                txt = font_sub.render(str(day), True, txt_col)
                screen.blit(txt, (x + 14, y + 8))

                # 有计划标记
                key = f"{year}-{month}-{day}"
                if key in plans and len(plans[key]) > 0:
                    dot = font_small.render("●", True, GREEN)
                    screen.blit(dot, (x + cell_w - 24, y + 8))

# ================== 绘制计划面板（精致卡片） ==================
def draw_plan_panel():
    panel_rect = pygame.Rect(60, 520, 780, 100)
    rounded_rect(screen, GRAY_LIGHT, panel_rect, 12)

    if selected_date:
        title = font_sub.render(f"📅 {selected_date} 计划", True, PURPLE)
        screen.blit(title, (80, 530))

        key = selected_date
        ps = plans.get(key, [])
        for i, p in enumerate(ps[:4]):
            line = font_text.render(f"• {p}", True, BLACK)
            screen.blit(line, (80, 565 + i * 22))
    else:
        tip = font_sub.render("请点击日期查看/管理计划", True, GRAY_DARK)
        screen.blit(tip, (80, 545))

# ================== 输入计划（高清界面） ==================
def get_input_text():
    input_text = ""
    inputing = True
    while inputing:
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                pygame.quit(); sys.exit()
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_RETURN:
                    inputing = False
                elif e.key == pygame.K_BACKSPACE:
                    input_text = input_text[:-1]
                else:
                    input_text += e.unicode

        screen.fill(BG)
        draw_calendar(current_year, current_month)
        draw_plan_panel()

        input_bg = pygame.Rect(60, 590, 780, 40)
        rounded_rect(screen, WHITE, input_bg, 10)
        pygame.draw.rect(screen, GRAY_MID, input_bg, 2, border_radius=10)

        tip = font_text.render("输入计划 →", True, BLUE)
        screen.blit(tip, (80, 595))
        text_surf = font_text.render(input_text, True, BLACK)
        screen.blit(text_surf, (190, 595))

        pygame.display.flip()
        clock.tick(FPS)
    return input_text.strip()

# ================== 主循环 ==================
def main():
    global selected_date, current_year, current_month

    while True:
        screen.fill(BG)
        mx, my = pygame.mouse.get_pos()

        draw_calendar(current_year, current_month)
        draw_plan_panel()

        # 按钮：添加计划
        btn_add = pygame.Rect(700, 525, 120, 36)
        add_col = BLUE_HOVER if btn_add.collidepoint(mx, my) else BLUE
        rounded_rect(screen, add_col, btn_add, 10)
        screen.blit(font_small.render("＋ 添加计划", True, WHITE), (718, 530))

        # 按钮：清空计划
        btn_clear = pygame.Rect(700, 570, 120, 36)
        clear_col = RED_HOVER if btn_clear.collidepoint(mx, my) else RED
        rounded_rect(screen, clear_col, btn_clear, 10)
        screen.blit(font_small.render("清空当日", True, WHITE), (718, 575))

        # 事件
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                pygame.quit(); sys.exit()

            if e.type == pygame.MOUSEBUTTONDOWN:
                # 选择日期
                x_start = 60
                y_start = 140
                cell_w = 108
                cell_h = 68
                cal = calendar.monthcalendar(current_year, current_month)
                hit_day = None

                for r, week in enumerate(cal):
                    for c, day in enumerate(week):
                        x = x_start + c * cell_w
                        y = y_start + r * cell_h
                        rect = pygame.Rect(x, y, cell_w - 2, cell_h - 2)
                        if rect.collidepoint(mx, my) and day != 0:
                            hit_day = day
                            break
                    if hit_day: break

                if hit_day:
                    selected_date = f"{current_year}-{current_month}-{hit_day}"

                # 添加计划
                if btn_add.collidepoint(mx, my) and selected_date:
                    txt = get_input_text()
                    if txt:
                        if selected_date not in plans:
                            plans[selected_date] = []
                        plans[selected_date].append(txt)

                # 清空计划
                if btn_clear.collidepoint(mx, my) and selected_date:
                    if selected_date in plans:
                        plans[selected_date] = []

        pygame.display.flip()
        clock.tick(FPS)

if __name__ == "__main__":
    main()