import pygame
import sys

# 初始化Pygame
pygame.init()

# -------------------------- 窗口设置 --------------------------
WIDTH, HEIGHT = 500, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("长度单位转换器")

# -------------------------- 颜色定义 --------------------------
WHITE = (255, 255, 255)
GRAY = (230, 230, 230)
BLUE = (50, 150, 255)
BLACK = (0, 0, 0)
RED = (255, 60, 60)

# -------------------------- 字体 --------------------------
font_large = pygame.font.SysFont("simhei", 32)  # 标题
font_mid = pygame.font.SysFont("simhei", 24)    # 按钮/标签
font_small = pygame.font.SysFont("simhei", 20)  # 输入提示

# -------------------------- 单位列表与换算关系 --------------------------
units = ["米", "厘米", "毫米", "千米", "英寸", "英尺"]
unit_values = {
    "米": 1.0,
    "厘米": 0.01,
    "毫米": 0.001,
    "千米": 1000.0,
    "英寸": 0.0254,
    "英尺": 0.3048
}

# -------------------------- 界面状态变量 --------------------------
input_text = ""               # 输入框内容
from_unit = 0                 # 原单位索引
to_unit = 1                   # 目标单位索引
result_text = "结果将在这里显示"
input_active = True           # 输入框是否激活

# -------------------------- 按钮区域 --------------------------
# 输入框
input_rect = pygame.Rect(100, 100, 300, 50)
# 原单位切换按钮
from_left_rect = pygame.Rect(80, 170, 40, 40)
from_right_rect = pygame.Rect(210, 170, 40, 40)
# 目标单位切换按钮
to_left_rect = pygame.Rect(280, 170, 40, 40)
to_right_rect = pygame.Rect(410, 170, 40, 40)
# 转换按钮
convert_rect = pygame.Rect(180, 230, 140, 50)

# -------------------------- 转换函数 --------------------------
def convert_length(value, from_u, to_u):
    try:
        # 统一转成米，再转目标单位
        meters = value * unit_values[from_u]
        result = meters / unit_values[to_u]
        return round(result, 6)  # 保留6位小数
    except:
        return "输入无效"

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

while running:
    screen.fill(WHITE)

    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # 键盘输入
        if event.type == pygame.KEYDOWN:
            if input_active:
                if event.key == pygame.K_RETURN:
                    # 按下回车执行转换
                    try:
                        val = float(input_text)
                        res = convert_length(val, units[from_unit], units[to_unit])
                        result_text = f"{val} {units[from_unit]} = {res} {units[to_unit]}"
                    except:
                        result_text = "请输入有效数字"
                elif event.key == pygame.K_BACKSPACE:
                    input_text = input_text[:-1]
                else:
                    # 只允许数字和小数点
                    if event.unicode in "0123456789.":
                        input_text += event.unicode

        # 鼠标点击
        if event.type == pygame.MOUSEBUTTONDOWN:
            # 点击输入框
            if input_rect.collidepoint(event.pos):
                input_active = True
            else:
                input_active = False

            # 原单位左
            if from_left_rect.collidepoint(event.pos):
                from_unit = (from_unit - 1) % len(units)
            # 原单位右
            if from_right_rect.collidepoint(event.pos):
                from_unit = (from_unit + 1) % len(units)

            # 目标单位左
            if to_left_rect.collidepoint(event.pos):
                to_unit = (to_unit - 1) % len(units)
            # 目标单位右
            if to_right_rect.collidepoint(event.pos):
                to_unit = (to_unit + 1) % len(units)

            # 转换按钮
            if convert_rect.collidepoint(event.pos):
                try:
                    val = float(input_text)
                    res = convert_length(val, units[from_unit], units[to_unit])
                    result_text = f"{val} {units[from_unit]} = {res} {units[to_unit]}"
                except:
                    result_text = "请输入有效数字"

    # ===================== 绘制界面 =====================
    # 标题
    title = font_large.render("长度单位转换器", True, BLUE)
    screen.blit(title, (WIDTH//2 - title.get_width()//2, 20))

    # 输入框
    pygame.draw.rect(screen, GRAY if input_active else WHITE, input_rect, border_radius=8)
    pygame.draw.rect(screen, BLUE, input_rect, 2, border_radius=8)
    input_surf = font_mid.render(input_text if input_text else "请输入数值", True, BLACK)
    screen.blit(input_surf, (input_rect.x + 10, input_rect.y + 8))

    # 原单位显示 + 切换按钮
    pygame.draw.rect(screen, BLUE, from_left_rect, border_radius=5)
    pygame.draw.rect(screen, BLUE, from_right_rect, border_radius=5)
    from_txt = font_mid.render("<", True, WHITE)
    screen.blit(from_txt, (from_left_rect.x+12, from_left_rect.y+5))
    screen.blit(from_txt, (from_right_rect.x+12, from_right_rect.y+5))
    from_unit_txt = font_mid.render(units[from_unit], True, BLACK)
    screen.blit(from_unit_txt, (135, 175))

    # 目标单位显示 + 切换按钮
    pygame.draw.rect(screen, BLUE, to_left_rect, border_radius=5)