import pygame
import sys

# 初始化
pygame.init()
W, H = 900, 600
screen = pygame.display.set_mode((W, H))
pygame.display.set_caption("侯氏制碱法演示 - Pygame")
clock = pygame.time.Clock()
font = pygame.font.SysFont("SimHei", 20)
font_big = pygame.font.SysFont("SimHei", 28)
font_small = pygame.font.SysFont("SimHei", 16)

# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (70, 130, 180)
GRAY = (200, 200, 200)
DARK_GRAY = (100, 100, 100)
RED = (200, 60, 60)
GREEN = (60, 160, 80)
YELLOW = (230, 200, 50)
CYAN = (100, 200, 220)
ORANGE = (240, 150, 50)

# 状态
step = 0  # 0: 原料 1: 反应塔 2: 加热分解 3: 副产品 4: 完成

def draw_text(text, x, y, color=BLACK, big=False, small=False):
    f = font_big if big else (font_small if small else font)
    surf = f.render(text, True, color)
    screen.blit(surf, (x, y))

def draw_arrow(x1, y1, x2, y2, color=DARK_GRAY, width=3):
    pygame.draw.line(screen, color, (x1, y1), (x2, y2), width)
    # 箭头头部
    import math
    angle = math.atan2(y2 - y1, x2 - x1)
    size = 12
    ax = x2 - size * math.cos(angle - 0.4)
    ay = y2 - size * math.sin(angle - 0.4)
    bx = x2 - size * math.cos(angle + 0.4)
    by = y2 - size * math.sin(angle + 0.4)
    pygame.draw.polygon(screen, color, [(x2, y2), (ax, ay), (bx, by)])

# 原料区域
raw_rect = pygame.Rect(40, 130, 200, 180)
# 反应塔
tower_rect = pygame.Rect(340, 80, 200, 260)
# 加热分解
heat_rect = pygame.Rect(640, 80, 220, 180)
# 副产品
byproduct_rect = pygame.Rect(340, 400, 520, 160)

def draw_raw_materials(active):
    color = GREEN if active else GRAY
    pygame.draw.rect(screen, WHITE, raw_rect)
    pygame.draw.rect(screen, color, raw_rect, 3)
    draw_text("① 原料", raw_rect.x + 10, raw_rect.y + 8, color, big=True)
    draw_text("饱和食盐水 (NaCl + H₂O)", raw_rect.x + 10, raw_rect.y + 50)
    draw_text("氨气 (NH₃)", raw_rect.x + 10, raw_rect.y + 80)
    draw_text("二氧化碳 (CO₂)", raw_rect.x + 10, raw_rect.y + 110)
    if active:
        draw_text("→ 通入反应塔", raw_rect.x + 10, raw_rect.y + 150, GREEN)
    # 小图标
    pygame.draw.circle(screen, CYAN, (raw_rect.x + 170, raw_rect.y + 60), 12)  # 水滴
    pygame.draw.circle(screen, YELLOW, (raw_rect.x + 170, raw_rect.y + 90), 12)  # 氨气
    pygame.draw.circle(screen, GRAY, (raw_rect.x + 170, raw_rect.y + 120), 12)   # CO2

def draw_tower(active):
    color = BLUE if active else GRAY
    pygame.draw.rect(screen, (230, 240, 250), tower_rect)
    pygame.draw.rect(screen, color, tower_rect, 4)
    draw_text("② 反应塔", tower_rect.x + 50, tower_rect.y + 8, color, big=True)
    # 塔内液体
    pygame.draw.rect(screen, (180, 220, 240), (tower_rect.x + 15, tower_rect.y + 80, tower_rect.w - 30, 100))
    # 气泡
    for i in range(5):
        pygame.draw.circle(screen, CYAN, (tower_rect.x + 50 + i*30, tower_rect.y + 130), 6)
    draw_text("NaCl + NH₃ + CO₂ + H₂O", tower_rect.x + 10, tower_rect.y + 195, BLACK, small=True)
    draw_text("→ NaHCO₃↓ + NH₄Cl", tower_rect.x + 10, tower_rect.y + 220, RED, small=True)
    if active:
        draw_text("反应进行中...", tower_rect.x + 50, tower_rect.y + 250, BLUE)

def draw_heat(active):
    color = ORANGE if active else GRAY
    pygame.draw.rect(screen, WHITE, heat_rect)
    pygame.draw.rect(screen, color, heat_rect, 3)
    draw_text("③ 加热分解", heat_rect.x + 10, heat_rect.y + 8, color, big=True)
    draw_text("2NaHCO₃ --Δ→", heat_rect.x + 10, heat_rect.y + 55)
    draw_text("Na₂CO₃ + H₂O + CO₂↑", heat_rect.x + 10, heat_rect.y + 85, RED)
    # 火焰图标
    if active:
        for i in range(3):
            pygame.draw.polygon(screen, ORANGE, [
                (heat_rect.x + 60 + i*40, heat_rect.y + 150),
                (heat_rect.x + 50 + i*40, heat_rect.y + 170),
                (heat_rect.x + 70 + i*40, heat_rect.y + 170)
            ])
        draw_text("加热中...", heat_rect.x + 70, heat_rect.y + 120, ORANGE)
    # 产物
    draw_text("纯碱 Na₂CO₃", heat_rect.x + 60, heat_rect.y + 150, GREEN, small=True)

def draw_byproduct(active):
    color = RED if active else GRAY
    pygame.draw.rect(screen, (255, 245, 245), byproduct_rect)
    pygame.draw.rect(screen, color, byproduct_rect, 3)
    draw_text("④ 副产品", byproduct_rect.x + 10, byproduct_rect.y + 8, color, big=True)
    draw_text("NH₄Cl (氯化铵) —— 可用作化肥", byproduct_rect.x + 10, byproduct_rect.y + 50)
    draw_text("CO₂ 可循环利用", byproduct_rect.x + 10, byproduct_rect.y + 80, GREEN)
    if active:
        draw_text("✓ 侯氏制碱法完成", byproduct_rect.x + 10, byproduct_rect.y + 115, RED, big=True)

def draw_flow_arrows():
    # 原料 → 反应塔
    draw_arrow(raw_rect.right + 5, raw_rect.centery, tower_rect.left - 5, tower_rect.centery)
    # 反应塔 → 加热
    draw_arrow(tower_rect.right + 5, tower_rect.y + 100, heat_rect.left - 5, heat_rect.y + 100)
    # 加热 → 副产品 (向下再向右)
    draw_arrow(heat_rect.right - 40, heat_rect.bottom + 5, heat_rect.right - 40, byproduct_rect.top - 5)
    # 反应塔 → 副产品 (向下)
    draw_arrow(tower_rect.centerx, tower_rect.bottom + 5, tower_rect.centerx, byproduct_rect.top - 5)

def draw_instructions():
    instructions = [
        "按 1：展示原料",
        "按 2：反应塔反应",
        "按 3：加热分解",
        "按 4：查看副产品",
        "按 R：重置",
        "按 ESC：退出",
    ]
    y = 20
    for ins in instructions:
        draw_text(ins, 20, y, DARK_GRAY, small=True)
        y += 20

# 主循环
running = True
while running:
    screen.fill((245, 245, 250))
    
    # 标题
    draw_text("侯氏制碱法（联合制碱法）演示", W//2 - 200, 15, BLUE, big=True)
    
    # 绘制各区域
    draw_raw_materials(step >= 1)
    draw_tower(step >= 2)
    draw_heat(step >= 3)
    draw_byproduct(step >= 4)
    draw_flow_arrows()
    draw_instructions()
    
    # 状态提示
    hints = {
        0: "按 1 开始：通入原料",
        1: "按 2 进入反应塔：生成 NaHCO₃ 和 NH₄Cl",
        2: "按 3 加热分解：NaHCO₃ 分解为纯碱",
        3: "按 4 查看副产品：NH₄Cl 和 CO₂",
        4: "全部完成！按 R 重置",
    }
    draw_text(hints.get(step, ""), W//2 - 180, H - 35, RED, big=True)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
            elif event.key == pygame.K_1:
                step = max(step, 1)
            elif event.key == pygame.K_2:
                step = max(step, 2)
            elif event.key == pygame.K_3:
                step = max(step, 3)
            elif event.key == pygame.K_4:
                step = max(step, 4)
            elif event.key == pygame.K_r:
                step = 0
    
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()