import pygame
import os

WIDTH, HEIGHT = 1000, 680
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("趣味课程表")
clock = pygame.time.Clock()

# -------------------字体加载（修复报错方案）-------------------
def get_font(size):
    try:
        path = r"C:\Windows\Fonts\simhei.ttf"
        return pygame.font.Font(path, size)
    except:
        return pygame.font.Font(None, size)

font = get_font(22)
title_font = get_font(36)

# -------------------颜色配置-------------------
WHITE = (255, 255, 255)
HEADER_BG = (60, 130, 210)
HEADER_TEXT = (255, 255, 255)
ROW1 = (242, 248, 255)
ROW2 = (255, 255, 255)
BLACK = (30, 30, 30)
GRAY_LINE = (170, 170, 170)
HOVER_COLOR = (255, 255, 160)  # 鼠标悬停高亮黄色

subject_color = {
    "语文": (255, 180, 180),
    "数学": (170, 210, 255),
    "英语": (220, 190, 255),
    "物理": (255, 230, 170),
    "化学": (180, 255, 220),
    "体育": (255, 210, 160),
    "美术": (255, 200, 230),
    "音乐": (230, 220, 255),
    "历史": (220, 210, 180),
    "地理": (200, 240, 200),
    "生物": (210, 250, 210),
    "信息": (200, 220, 255),
    "自习": (245, 245, 245),
    "班会": (250, 220, 200),
}

week_title = ["节次", "周一", "周二", "周三", "周四", "周五"]
course_data = [
    ["第1节", "语文", "数学", "英语", "物理", "化学"],
    ["第2节", "数学", "语文", "历史", "生物", "地理"],
    ["第3节", "英语", None, "数学", "语文", None],
    ["第4节", "体育", "美术", "音乐", None, "信息"],
    ["第5节", "自习", "数学", None, "英语", "语文"],
    ["第6节", "物理", "化学", "地理", "历史", "自习"],
    ["第7节", None, "班会", None, None, None],
]

col_num = len(week_title)
row_num = len(course_data)
cell_w = WIDTH // col_num
cell_h = (HEIGHT - 80) // row_num
TOP_MARGIN = 80
hover_row = -1
hover_col = -1
RADIUS = 8  # 圆角大小

def draw_dashed_line(surf, color, start, end, dash_length=10):
    x1, y1 = start
    x2, y2 = end
    dist = ((x2-x1)**2 + (y2-y1)**2)**0.5
    steps = int(dist // dash_length)
    for i in range(0, steps, 2):
        t1 = i / steps
        t2 = (i+1)/steps
        sx = x1 + (x2-x1)*t1
        sy = y1 + (y2-y1)*t1
        ex = x1 + (x2-x1)*t2
        ey = y1 + (y2-y1)*t2
        pygame.draw.line(surf, color, (sx, sy), (ex, ey), 3)

def draw_table():
    global hover_row, hover_col
    screen.fill(WHITE)
    # 标题
    title_surf = title_font.render("✨趣味课程表✨", True, BLACK)
    screen.blit(title_surf, ((WIDTH - title_surf.get_width())//2, 20))

    # 表头
    for i, text in enumerate(week_title):
        rect = pygame.Rect(i*cell_w, TOP_MARGIN, cell_w, cell_h)
        pygame.draw.rect(screen, HEADER_BG, rect, border_radius=RADIUS)
        pygame.draw.rect(screen, BLACK, rect, 2, border_radius=RADIUS)
        text_surf = font.render(text, True, HEADER_TEXT)
        tx = rect.x + (cell_w - text_surf.get_width())//2
        ty = rect.y + (cell_h - text_surf.get_height())//2
        screen.blit(text_surf, (tx, ty))

    # 课程格子
    for r, row in enumerate(course_data):
        bg_color = ROW1 if r % 2 == 0 else ROW2
        for c, text in enumerate(row):
            rect = pygame.Rect(c*cell_w, TOP_MARGIN + (r+1)*cell_h, cell_w, cell_h)
            # 判断鼠标悬停
            is_hover = (r == hover_row and c == hover_col)
            if is_hover:
                fill_color = HOVER_COLOR
            elif text and text in subject_color:
                fill_color = subject_color[text]
            else:
                fill_color = bg_color

            pygame.draw.rect(screen, fill_color, rect, border_radius=RADIUS)
            pygame.draw.rect(screen, GRAY_LINE, rect, 1, border_radius=RADIUS)
            if text is not None:
                text_surf = font.render(text, True, BLACK)
                tx = rect.x + (cell_w - text_surf.get_width())//2
                ty = rect.y + (cell_h - text_surf.get_height())//2
                screen.blit(text_surf, (tx, ty))

    # 午休虚线分割线（第4节课之后）
    split_y = TOP_MARGIN + 5*cell_h
    draw_dashed_line(screen, (100, 100, 100), (0, split_y), (WIDTH, split_y))
    text_rest = font.render("午休", True, (80,80,80))
    screen.blit(text_rest, (20, split_y+5))

# 主循环
running = True
while running:
    mx, my = pygame.mouse.get_pos()
    hover_row = -1
    hover_col = -1
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 检测鼠标在哪一格
    if TOP_MARGIN < my < HEIGHT:
        r = (my - TOP_MARGIN)//cell_h - 1
        c = mx // cell_w
        if 0 <= r < row_num and 0 <= c < col_num:
            hover_row = r
            hover_col = c

    draw_table()
    pygame.display.flip()

pygame.quit()
