import pygame
pygame.init()

# 窗口设置
WIDTH, HEIGHT = 600, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("货币单位转换器")
clock = pygame.time.Clock()

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
BLUE = (50, 120, 220)
RED = (220, 60, 60)

# 字体
font_big = pygame.font.SysFont("simhei", 32)
font_text = pygame.font.SysFont("simhei", 26)

# 汇率（实时可自行更新）
exchange_rate = {
    "CNY": 1,      # 人民币
    "USD": 0.138,  # 美元
    "EUR": 0.127,  # 欧元
    "JPY": 20.45,  # 日元
    "GBP": 0.109   # 英镑
}
currency_list = list(exchange_rate.keys())
select_from = 0
select_to = 1

# 输入相关
input_text = ""
result_text = ""
active_input = True

# 按钮区域
input_rect = pygame.Rect(80, 80, 440, 45)
calc_btn = pygame.Rect(80, 260, 180, 50)
clear_btn = pygame.Rect(340, 260, 180, 50)

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

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

        # 鼠标点击
        if event.type == pygame.MOUSEBUTTONDOWN:
            if input_rect.collidepoint(mouse_pos):
                active_input = True
            else:
                active_input = False

            # 转换按钮
            if calc_btn.collidepoint(mouse_pos):
                try:
                    money = float(input_text)
                    cur_from = currency_list[select_from]
                    cur_to = currency_list[select_to]
                    # 换算逻辑
                    cny_value = money / exchange_rate[cur_from]
                    target = cny_value * exchange_rate[cur_to]
                    result_text = f"{money} {cur_from} = {target:.2f} {cur_to}"
                except ValueError:
                    result_text = "请输入有效数字！"

            # 清空按钮
            if clear_btn.collidepoint(mouse_pos):
                input_text = ""
                result_text = ""

        # 键盘输入
        if event.type == pygame.KEYDOWN and active_input:
            if event.key == pygame.K_BACKSPACE:
                input_text = input_text[:-1]
            elif event.key == pygame.K_RETURN:
                pass
            else:
                # 只允许数字和小数点
                if event.unicode in "0123456789.":
                    input_text += event.unicode

    # 绘制输入框
    pygame.draw.rect(screen, GRAY, input_rect, 2)
    text_surface = font_text.render(input_text, True, BLACK)
    screen.blit(text_surface, (input_rect.x + 10, input_rect.y + 5))

    # 币种选择文字
    text_from = font_text.render(f"源货币：{currency_list[select_from]}  点击切换", True, BLACK)
    text_to = font_text.render(f"目标货币：{currency_list[select_to]}  点击切换", True, BLACK)
    screen.blit(text_from, (80, 140))
    screen.blit(text_to, (80, 180))

    # 简易切换币种（按左右键切换）
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        select_from = (select_from - 1) % len(currency_list)
    if keys[pygame.K_RIGHT]:
        select_to = (select_to + 1) % len(currency_list)

    # 计算按钮
    pygame.draw.rect(screen, BLUE, calc_btn)
    calc_text = font_text.render("开始转换", True, WHITE)
    screen.blit(calc_text, (calc_btn.x + 45, calc_btn.y + 8))

    # 清空按钮
    pygame.draw.rect(screen, RED, clear_btn)
    clear_text = font_text.render("清空", True, WHITE)
    screen.blit(clear_text, (clear_btn.x + 65, clear_btn.y + 8))

    # 输出结果
    res_surface = font_big.render(result_text, True, (0, 100, 0))
    screen.blit(res_surface, (80, 330))

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

pygame.quit()
