import pygame
import random
import sys

pygame.init()

WIDTH, HEIGHT = 700, 420
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("打字练习器")

# 颜色
BG_COLOR = (28, 28, 30)
TEXT_NORMAL = (220, 220, 220)
TEXT_CORRECT = (80, 220, 100)
TEXT_WRONG = (255, 80, 80)
INPUT_BG = (45, 45, 50)

# 字体容错
try:
    font_large = pygame.font.SysFont("simhei", 42)
    font_mid = pygame.font.SysFont("simhei", 32)
    font_small = pygame.font.SysFont("simhei", 24)
except:
    font_large = pygame.font.Font(None, 42)
    font_mid = pygame.font.Font(None, 32)
    font_small = pygame.font.Font(None, 24)

# 词库
word_list = [
    "python", "keyboard", "practice", "speed", "program",
    "window", "screen", "game", "function", "variable",
    "loop", "string", "number", "object", "import",
    "random", "pygame", "render", "display", "clock"
]

def get_new_word():
    return random.choice(word_list)

target_word = get_new_word()
user_input = ""
start_time = None
total_chars = 0
correct_chars = 0
game_running = True
clock = pygame.time.Clock()
FPS = 60

while True:
    now = pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if not game_running:
                # 重新开始
                target_word = get_new_word()
                user_input = ""
                start_time = None
                total_chars = 0
                correct_chars = 0
                game_running = True
                continue

            if event.key == pygame.K_BACKSPACE:
                if len(user_input) > 0:
                    user_input = user_input[:-1]
            elif event.key == pygame.K_RETURN:
                # 回车提交
                if start_time is None:
                    start_time = now
                # 统计字符
                min_len = min(len(user_input), len(target_word))
                for i in range(min_len):
                    total_chars += 1
                    if user_input[i] == target_word[i]:
                        correct_chars += 1
                # 换下一个
                target_word = get_new_word()
                user_input = ""
            else:
                char = event.unicode
                if char.isprintable():
                    if start_time is None:
                        start_time = now
                    user_input += char

    screen.fill(BG_COLOR)

    if game_running:
        # 绘制目标单词，逐字变色
        x = 80
        for idx, c in enumerate(target_word):
            if idx < len(user_input):
                if user_input[idx] == c:
                    color = TEXT_CORRECT
                else:
                    color = TEXT_WRONG
            else:
                color = TEXT_NORMAL
            surf = font_large.render(c, True, color)
            screen.blit(surf, (x, 100))
            x += surf.get_width() + 4

        # 当前输入框
        pygame.draw.rect(screen, INPUT_BG, (75, 180, 550, 50), border_radius=8)
        input_surf = font_mid.render(user_input, True, TEXT_NORMAL)
        screen.blit(input_surf, (85, 188))

        # 计算数据
        wpm_text = "WPM: 0"
        acc_text = "Accuracy: 0%"
        if start_time is not None:
            elapsed_min = (now - start_time) / 1000 / 60
            if elapsed_min > 0:
                words = correct_chars / 5
                wpm = round(words / elapsed_min)
                wpm_text = f"WPM: {wpm}"
            if total_chars > 0:
                acc = round(correct_chars / total_chars * 100)
                acc_text = f"Accuracy: {acc}%"

        wpm_surf = font_small.render(wpm_text, True, TEXT_NORMAL)
        acc_surf = font_small.render(acc_text, True, TEXT_NORMAL)
        hint = font_small.render("Enter 提交 | Backspace 删除", True, (150,150,150))

        screen.blit(wpm_surf, (80, 270))
        screen.blit(acc_surf, (220, 270))
        screen.blit(hint, (80, 320))

        # 判断是否打完全对
        if user_input == target_word:
            pass
    else:
        over = font_large.render("练习结束", True, TEXT_NORMAL)
        screen.blit(over, (WIDTH//2 - 100, HEIGHT//2 - 60))

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