import pygame
import math
import random
import time
import sys

# -------------------- 初始化 --------------------
pygame.init()
WIDTH, HEIGHT = 520, 560
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Wheel of Fortune")

FONT = pygame.font.Font(None, 28)
BIG_FONT = pygame.font.Font(None, 48)

# -------------------- 颜色 --------------------
BG = (30, 30, 30)
WHITE = (255, 255, 255)
YELLOW = (255, 215, 0)
RED = (220, 50, 50)
GREEN = (50, 200, 100)
BLUE = (50, 120, 220)
PURPLE = (160, 80, 220)
ORANGE = (255, 140, 0)
CYAN = (0, 200, 200)
PINK = (255, 100, 180)

COLORS = [RED, GREEN, BLUE, ORANGE, PURPLE, CYAN, YELLOW, PINK]

# -------------------- 奖品 --------------------
PRIZES = [
    "iPhone 15",
    "AirPods Pro",
    "1000 Points",
    "Thank You",
    "MacBook Pro",
    "50 Coupon",
    "iPad Mini",
    "Try Again"
]

SECTOR_ANGLE = 360 // len(PRIZES)
CENTER = (WIDTH // 2, 260)
RADIUS = 170

# -------------------- 状态 --------------------
angle = 0
spinning = False
speed = 0
target_angle = 0
result = ""
popup = False

spin_btn = pygame.Rect(190, 460, 140, 50)
close_btn = pygame.Rect(210, 340, 100, 40)

# -------------------- 粒子系统 --------------------
particles = []

def spawn_particles():
    particles.clear()
    for _ in range(60):
        particles.append({
            "x": CENTER[0],
            "y": CENTER[1],
            "vx": random.uniform(-4, 4),
            "vy": random.uniform(-4, 4),
            "life": 60,
            "color": random.choice(COLORS)
        })

def update_particles():
    for p in particles[:]:
        p["x"] += p["vx"]
        p["y"] += p["vy"]
        p["life"] -= 1
        if p["life"] <= 0:
            particles.remove(p)

# -------------------- 绘制转盘 --------------------
def draw_wheel():
    for i, prize in enumerate(PRIZES):
        start = math.radians(angle + i * SECTOR_ANGLE)
        end = math.radians(angle + (i + 1) * SECTOR_ANGLE)

        points = [CENTER]
        for a in [start, end]:
            x = CENTER[0] + RADIUS * math.cos(a)
            y = CENTER[1] + RADIUS * math.sin(a)
            points.append((x, y))

        pygame.draw.polygon(screen, COLORS[i], points)
        pygame.draw.polygon(screen, WHITE, points, 2)

        # 文字
        tx = CENTER[0] + (RADIUS - 50) * math.cos((start + end) / 2)
        ty = CENTER[1] + (RADIUS - 50) * math.sin((start + end) / 2)
        txt = pygame.font.Font(None, 22).render(prize, True, WHITE)
        screen.blit(txt, txt.get_rect(center=(tx, ty)))

    # 中心圆
    pygame.draw.circle(screen, YELLOW, CENTER, 36)
    pygame.draw.circle(screen, WHITE, CENTER, 36, 3)

# -------------------- 指针 --------------------
def draw_pointer():
    pts = [
        (CENTER[0], CENTER[1] - RADIUS - 20),
        (CENTER[0] - 14, CENTER[1] - RADIUS + 10),
        (CENTER[0] + 14, CENTER[1] - RADIUS + 10)
    ]
    pygame.draw.polygon(screen, YELLOW, pts)
    pygame.draw.polygon(screen, WHITE, pts, 2)

# -------------------- 灯泡环 --------------------
bulb_timer = 0

def draw_bulbs():
    global bulb_timer
    bulb_timer += 1
    for i in range(24):
        a = math.radians(i * 15 + angle)
        x = CENTER[0] + (RADIUS + 18) * math.cos(a)
        y = CENTER[1] + (RADIUS + 18) * math.sin(a)
        color = YELLOW if (i + int(bulb_timer / 10)) % 2 == 0 else WHITE
        pygame.draw.circle(screen, color, (int(x), int(y)), 6)

# -------------------- 弹窗 --------------------
def draw_popup():
    overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
    overlay.fill((0, 0, 0, 180))
    screen.blit(overlay, (0, 0))

    pygame.draw.rect(screen, BG, (100, 200, 320, 180), border_radius=16)
    pygame.draw.rect(screen, YELLOW, (100, 200, 320, 180), 3, border_radius=16)

    title = BIG_FONT.render("CONGRATS!", True, YELLOW)
    screen.blit(title, title.get_rect(center=(WIDTH // 2, 250)))

    res = FONT.render(result, True, WHITE)
    screen.blit(res, res.get_rect(center=(WIDTH // 2, 295)))

    pygame.draw.rect(screen, GREEN, close_btn, border_radius=8)
    btn_txt = FONT.render("AWESOME", True, WHITE)
    screen.blit(btn_txt, btn_txt.get_rect(center=close_btn.center))

# -------------------- 主循环 --------------------
clock = pygame.time.Clock()
running = True

while running:
    screen.fill(BG)
    mouse_pos = pygame.mouse.get_pos()

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

        elif event.type == pygame.MOUSEBUTTONDOWN:
            if spin_btn.collidepoint(event.pos) and not spinning:
                spinning = True
                speed = 25
                target_angle = angle + random.randint(1800, 3600)
                result = ""

            if popup and close_btn.collidepoint(event.pos):
                popup = False

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and not spinning:
                spinning = True
                speed = 25
                target_angle = angle + random.randint(1800, 3600)
                result = ""
            if event.key == pygame.K_ESCAPE:
                popup = False

    # 旋转逻辑（缓出）
    if spinning:
        angle += speed
        distance = target_angle - angle
        speed = max(0, distance / 40)
        if speed <= 0.05:
            spinning = False
            index = int(((angle + 90) % 360) // SECTOR_ANGLE)
            result = PRIZES[index]
            popup = True
            spawn_particles()

    draw_wheel()
    draw_bulbs()
    draw_pointer()
    update_particles()

    # 粒子
    for p in particles:
        alpha = max(0, min(255, p["life"] * 4))
        s = pygame.Surface((6, 6), pygame.SRCALPHA)
        pygame.draw.circle(s, (*p["color"], alpha), (3, 3), 3)
        screen.blit(s, (p["x"], p["y"]))

    # SPIN 按钮
    hover = spin_btn.collidepoint(mouse_pos)
    pygame.draw.rect(screen, YELLOW if hover else (200, 180, 0), spin_btn, border_radius=12)
    btn_txt = FONT.render("SPIN", True, BG)
    screen.blit(btn_txt, btn_txt.get_rect(center=spin_btn.center))

    # 提示
    hint = pygame.font.Font(None, 22).render("Press SPACE or Click SPIN", True, WHITE)
    screen.blit(hint, (WIDTH // 2 - 130, 520))

    if popup:
        draw_popup()

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

pygame.quit()
sys.exit()