import pygame
import sys

# 初始化Pygame
pygame.init()

# 窗口设置
WIDTH, HEIGHT = 500, 300
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("学生证件生成器")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 100, 200)
LIGHT_BLUE = (200, 220, 255)
GRAY = (150, 150, 150)

# ----------------- 终极修复：不用系统字体，用Pygame内置字体 -----------------
# 彻底解决版本兼容 + 编辑器环境报错
font_title = pygame.font.Font(None, 40)
font_text = pygame.font.Font(None, 30)
font_input = pygame.font.Font(None, 28)

# 学生信息（输入项）
student_info = {
    "title": "Student ID",
    "name": "",
    "id": "",
    "gender": "",
    "major": "",
    "college": ""
}

# 输入状态控制（当前正在输入的项）
input_index = 0
input_labels = ["Name:", "ID:", "Gender:", "Major:", "College:"]
input_values = ["", "", "", "", ""]
active = True  # 是否处于输入模式

# 主循环时钟
clock = pygame.time.Clock()

# 绘制学生证（核心函数）
def draw_id_card():
    # 清空屏幕
    screen.fill(LIGHT_BLUE)
    
    # 1. 绘制证件边框
    pygame.draw.rect(screen, BLUE, (20, 20, WIDTH-40, HEIGHT-40), 3)
    
    # 2. 绘制标题
    title_surf = font_title.render(student_info["title"], True, BLUE)
    screen.blit(title_surf, (WIDTH//2 - title_surf.get_width()//2, 30))
    
    # 3. 绘制学生信息（输入完成后展示）
    if not active:
        texts = [
            f"Name: {input_values[0]}",
            f"ID: {input_values[1]}",
            f"Gender: {input_values[2]}",
            f"Major: {input_values[3]}",
            f"College: {input_values[4]}"
        ]
        y_pos = 100
        for text in texts:
            text_surf = font_text.render(text, True, BLACK)
            screen.blit(text_surf, (50, y_pos))
            y_pos += 40
    
    # 4. 输入模式：绘制输入框
    else:
        y_pos = 100
        for i, label in enumerate(input_labels):
            # 标签文字
            label_surf = font_input.render(label, True, BLACK)
            screen.blit(label_surf, (50, y_pos))
            
            # 输入框背景
            pygame.draw.rect(screen, WHITE, (130, y_pos, 200, 30))
            pygame.draw.rect(screen, GRAY, (130, y_pos, 200, 30), 1)
            
            # 输入内容
            input_surf = font_input.render(input_values[i], True, BLACK)
            screen.blit(input_surf, (135, y_pos + 5))
            
            # 当前选中项高亮
            if i == input_index:
                pygame.draw.rect(screen, BLUE, (130, y_pos, 200, 30), 2)
        
        tip = font_input.render("TAB = next | ENTER = confirm | S = save", True, BLUE)
        screen.blit(tip, (50, HEIGHT - 40))

# 保存学生证为图片
def save_card():
    try:
        pygame.image.save(screen, "student_id.png")
        print("✅ Saved successfully: student_id.png")
    except:
        print("❌ Save failed")

# 主程序循环
running = True
while running:
    screen.fill(WHITE)
    draw_id_card()
    
    # 事件监听
    for event in pygame.event.get():
        # 关闭窗口
        if event.type == pygame.QUIT:
            running = False
        
        # 键盘输入（仅输入模式生效）
        if event.type == pygame.KEYDOWN and active:
            # 退格删除
            if event.key == pygame.K_BACKSPACE:
                input_values[input_index] = input_values[input_index][:-1]
            
            # TAB 切换输入项
            elif event.key == pygame.K_TAB:
                input_index = (input_index + 1) % 5
            
            # 回车确认，生成证件
            elif event.key == pygame.K_RETURN:
                active = False
            
            # 普通字符输入
            else:
                if event.unicode.isprintable() and len(input_values[input_index]) < 12:
                    input_values[input_index] += event.unicode
        
        # 按下 S 键保存图片
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_s:
                save_card()

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

pygame.quit()
sys.exit()