import pygame
import random
import sys

# 初始化
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("🎲 随机点名/抽签工具 - 完美版")

# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (50, 150, 255)
RED = (255, 80, 80)
GRAY = (200, 200, 200)
LIGHT_BLUE = (100, 200, 255)
GREEN = (0, 200, 100)

# 字体
font_large = pygame.font.SysFont("SimHei", 60)
font_mid = pygame.font.SysFont("SimHei", 35)
font_small = pygame.font.SysFont("SimHei", 25)

# 数据
name_list = []
selected_names = []
current_name = "等待抽取..."
is_rolling = False
roll_speed = 40
roll_counter = 0

# 按钮
btn_roll = pygame.Rect(150, 450, 200, 70)
btn_reset = pygame.Rect(450, 450, 200, 70)
btn_add = pygame.Rect(50, 350, 200, 40)
input_box = pygame.Rect(50, 300, 600, 40)

input_text = ""
active_input = False

clock = pygame.time.Clock()
running = True
pygame.key.start_text_input()

while running:
    screen.fill(WHITE)
    mx, my = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.TEXTINPUT:
            if active_input:
                input_text += event.text

        if event.type == pygame.KEYDOWN:
            if active_input:
                if event.key == pygame.K_BACKSPACE:
                    input_text = input_text[:-1]
                if event.key == pygame.K_RETURN:
                    new_name = input_text.strip()
                    if new_name and new_name not in name_list:
                        name_list.append(new_name)
                        input_text = ""

        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            if input_box.collidepoint(mx, my):
                active_input = True
            else:
                active_input = False

            if btn_roll.collidepoint(mx, my):
                rest = [n for n in name_list if n not in selected_names]
                if rest:
                    is_rolling = not is_rolling
                    if not is_rolling and current_name not in selected_names:
                        selected_names.append(current_name)

            if btn_reset.collidepoint(mx, my):
                selected_names.clear()
                current_name = "等待抽取..."
                is_rolling = False

            if btn_add.collidepoint(mx, my):
                new_name = input_text.strip()
                if new_name and new_name not in name_list:
                    name_list.append(new_name)
                    input_text = ""

    if is_rolling:
        rest_names = [n for n in name_list if n not in selected_names]
        if rest_names:
            roll_counter += 1
            if roll_counter >= roll_speed:
                current_name = random.choice(rest_names)
                roll_counter = 0

    title = font_mid.render("随机点名 / 抽签工具", True, BLUE)
    screen.blit(title, (WIDTH//2 - title.get_width()//2, 20))

    pygame.draw.rect(screen, LIGHT_BLUE if active_input else GRAY, input_box, 2)
    input_surf = font_small.render(input_text, True, BLACK)
    screen.blit(input_surf, (input_box.x + 8, input_box.y + 5))

    pygame.draw.rect(screen, GREEN, btn_add)
    add_txt = font_small.render("添加名单", True, WHITE)
    screen.blit(add_txt, (btn_add.x + 60, btn_add.y + 5))

    res_rect = pygame.Rect(50, 100, 700, 150)
    pygame.draw.rect(screen, GRAY, res_rect, border_radius=10)
    name_surf = font_large.render(current_name, True, BLACK)
    screen.blit(name_surf, (WIDTH//2 - name_surf.get_width()//2, 130))

    pygame.draw.rect(screen, BLUE, btn_roll, border_radius=10)
    roll_txt = font_mid.render("停止抽取" if is_rolling else "开始抽取", True, WHITE)
    screen.blit(roll_txt, (btn_roll.x + 30, btn_roll.y + 10))

    pygame.draw.rect(screen, RED, btn_reset, border_radius=10)
    reset_txt = font_mid.render("重置", True, WHITE)
    screen.blit(reset_txt, (btn_reset.x + 70, btn_reset.y + 10))

    left_cnt = len(name_list) - len(selected_names)
    un_text = font_small.render(f"未抽取 ({left_cnt}):", True, BLACK)
    screen.blit(un_text, (50, 520))
    un_list = [n for n in name_list if n not in selected_names]
    un_str = "  ".join(un_list[:6])
    un_surf = font_small.render(un_str, True, BLACK)
    screen.blit(un_surf, (50, 550))

    sel_text = font_small.render(f"已抽取 ({len(selected_names)}):", True, RED)
    screen.blit(sel_text, (400, 520))
    sel_str = "  ".join(selected_names[-6:])
    sel_surf = font_small.render(sel_str, True, RED)
    screen.blit(sel_surf, (400, 550))

    pygame.display.update()
    clock.tick(60)

pygame.key.stop_text_input()
pygame.quit()
sys.exit()