import pygame
import math
import sys

# 初始化pygame
pygame.init()

# 窗口配置
WIDTH, HEIGHT = 600, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("分数约分工具")
clock = pygame.time.Clock()

# 颜色
BG_COLOR = (30, 30, 50)
BOX_COLOR = (60,60,90)
TEXT_COLOR = (255,255,255)
BTN_COLOR = (80, 140, 200)
BTN_HOVER = (110, 170, 230)

# 字体
font_small = pygame.font.SysFont("simhei",24)
font_mid = pygame.font.SysFont("simhei",32)

# 输入框数据
input_text = ["", ""]  # 分子，分母
active_box = 0
result_str = ""
gcd_val = 0

# 按钮矩形
calc_btn = pygame.Rect(80,280,180,50)
reset_btn = pygame.Rect(340,280,180,50)

def reduce_fraction(numer, denom):
    """分数约分核心函数"""
    n = int(numer)
    d = int(denom)
    if d == 0:
        return "错误：分母不能为0",0
    g = math.gcd(abs(n),abs(d))
    sn = n // g
    sd = d // g
    return f"最简分数：{sn}/{sd} ,最大公约数={g}",g

running = True
while running:
    mx, my = pygame.mouse.get_pos()
    screen.fill(BG_COLOR)

    # 事件监听
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        # 鼠标点击切换输入框 / 按钮
        if event.type == pygame.MOUSEBUTTONDOWN:
            box1 = pygame.Rect(160,100,120,45)
            box2 = pygame.Rect(320,100,120,45)
            if box1.collidepoint(mx,my):
                active_box = 0
            elif box2.collidepoint(mx,my):
                active_box = 1
            # 计算按钮
            if calc_btn.collidepoint(mx,my):
                if input_text[0] and input_text[1]:
                    result_str,gcd_val = reduce_fraction(input_text[0],input_text[1])
            # 重置按钮
            if reset_btn.collidepoint(mx,my):
                input_text = ["",""]
                result_str = ""
                gcd_val = 0
        # 键盘输入数字
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_BACKSPACE:
                input_text[active_box] = input_text[active_box][:-1]
            else:
                ch = event.unicode
                if ch in "0123456789-":
                    input_text[active_box] += ch

    # 绘制输入框
    box1 = pygame.Rect(160,100,120,45)
    box2 = pygame.Rect(320,100,120,45)
    pygame.draw.rect(screen,BOX_COLOR,box1,border_radius=6)
    pygame.draw.rect(screen,BOX_COLOR,box2,border_radius=6)

    t1 = font_small.render("分子",True,TEXT_COLOR)
    screen.blit(t1,(70,108))
    t2 = font_small.render("分母",True,TEXT_COLOR)
    screen.blit(t2,(270,108))

    num_text = font_mid.render(input_text[0],True,TEXT_COLOR)
    den_text = font_mid.render(input_text[1],True,TEXT_COLOR)
    screen.blit(num_text,(box1.x+10,105))
    screen.blit(den_text,(box2.x+10,105))

    # 绘制按钮
    c_color = BTN_HOVER if calc_btn.collidepoint(mx,my) else BTN_COLOR
    r_color = BTN_HOVER if reset_btn.collidepoint(mx,my) else BTN_COLOR
    pygame.draw.rect(screen,c_color,calc_btn,border_radius=8)
    pygame.draw.rect(screen,r_color,reset_btn,border_radius=8)

    screen.blit(font_small.render("开始约分",True,(255,255,255)),(calc_btn.x+45,292))
    screen.blit(font_small.render("重置",True,(255,255,255)),(reset_btn.x+65,292))

    # 输出结果文字
    res_surf = font_small.render(result_str,True,(80,255,120))
    screen.blit(res_surf,(60,180))

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

pygame.quit()
sys.exit()