import pygame
import sys

# 初始化Pygame
pygame.init()

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

# 颜色定义（RGB）
WHITE = (255, 255, 255)
GRAY = (200, 200, 200)
BLUE = (50, 150, 255)
BLACK = (0, 0, 0)
RED = (255, 80, 80)
LIGHT_BLUE = (173, 216, 230)

# 字体设置
font = pygame.font.SysFont("SimHei", 24)  # 支持中文
small_font = pygame.font.SysFont("SimHei", 20)

# 汇率（1人民币 = 对应货币）
# 来源：实时参考汇率
exchange_rates = {
    "人民币": 1.0,
    "美元": 0.138,
    "欧元": 0.128,
    "英镑": 0.109,
    "日元": 21.6,
    "韩元": 184.5
}

# 货币列表
currencies = list(exchange_rates.keys())

# 界面组件初始值
input_text = ""  # 输入框内容
from_currency = "人民币"  # 原货币
to_currency = "美元"  # 目标货币
result_text = "转换结果："  # 结果显示

# 按钮/输入框坐标与尺寸
input_rect = pygame.Rect(50, 80, 280, 40)
convert_btn = pygame.Rect(150, 280, 200, 45)
clear_btn = pygame.Rect(340, 80, 100, 40)

# 下拉菜单选择区域
from_rect = pygame.Rect(50, 150, 180, 40)
to_rect = pygame.Rect(270, 150, 180, 40)

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

while running:
    screen.fill(LIGHT_BLUE)  # 背景色

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

        # 键盘输入（只允许数字和小数点）
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_BACKSPACE:
                input_text = input_text[:-1]
            elif event.unicode.isdigit() or event.unicode == ".":
                # 限制只能有一个小数点
                if event.unicode == "." and "." in input_text:
                    pass
                else:
                    input_text += event.unicode

        # 鼠标点击
        if event.type == pygame.MOUSEBUTTONDOWN:
            # 转换按钮
            if convert_btn.collidepoint(event.pos):
                try:
                    amount = float(input_text)
                    # 换算公式：目标金额 = 原金额 * (目标汇率 / 原汇率)
                    result = amount * (exchange_rates[to_currency] / exchange_rates[from_currency])
                    result_text = f"转换结果：{result:.2f} {to_currency}"
                except:
                    result_text = "请输入有效数字！"

            # 清空按钮
            if clear_btn.collidepoint(event.pos):
                input_text = ""
                result_text = "转换结果："

            # 选择原货币
            if from_rect.collidepoint(event.pos):
                idx = currencies.index(from_currency)
                from_currency = currencies[(idx + 1) % len(currencies)]

            # 选择目标货币
            if to_rect.collidepoint(event.pos):
                idx = currencies.index(to_currency)
                to_currency = currencies[(idx + 1) % len(currencies)]

    # ———————— 绘制界面 ————————
    # 标题
    title = font.render("货币单位转换器", True, BLACK)
    screen.blit(title, (WIDTH//2 - title.get_width()//2, 20))

    # 输入框
    pygame.draw.rect(screen, WHITE, input_rect, border_radius=5)
    input_surf = font.render(input_text, True, BLACK)
    screen.blit(input_surf, (input_rect.x + 5, input_rect.y + 5))

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

    # 原货币选择框
    pygame.draw.rect(screen, GRAY, from_rect, border_radius=5)
    from_text = small_font.render(f"原货币：{from_currency}", True, BLACK)
    screen.blit(from_text, (from_rect.x + 10, from_rect.y + 8))

    # 目标货币选择框
    pygame.draw.rect(screen, GRAY, to_rect, border_radius=5)
    to_text = small_font.render(f"目标货币：{to_currency}", True, BLACK)
    screen.blit(to_text, (to_rect.x + 10, to_rect.y + 8))

    # 转换按钮
    pygame.draw.rect(screen, BLUE, convert_btn, border_radius=8)
    convert_text = font.render("点击转换", True, WHITE)
    screen.blit(convert_text, (convert_btn.x + 55, convert_btn.y + 8))

    # 结果显示
    result_surf = font.render(result_text, True, BLACK)
    screen.blit(result_surf, (50, 220))

    # 刷新界面
    pygame.display.flip()
    clock.tick(30)

pygame.quit()
sys.exit()