import pygame
import math

# Initialize pygame
pygame.init()

# Window settings
WIDTH, HEIGHT = 600, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Fraction Reducer")
clock = pygame.time.Clock()

# Color definitions
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (180, 180, 180)
LIGHT_GRAY = (230, 230, 230)
BLUE = (50, 100, 200)
RED = (200, 50, 50)

# Font setup
try:
    font = pygame.font.SysFont("arial", 32)
    small_font = pygame.font.SysFont("arial", 24)
except:
    font = pygame.font.Font(None,32)
    small_font = pygame.font.Font(None,24)

# Input variables
numerator_text = ""
denominator_text = ""
active_input = "num"  # "num" for numerator, "den" for denominator
result_text = ""
error_text = ""

def reduce_fraction(num, den):
    """Reduce fraction to simplest form, return result string and error message"""
    if den == 0:
        return None, "Denominator cannot be 0!"
    g = math.gcd(abs(num), abs(den))
    r_num = num // g
    r_den = den // g
    if r_den == 1:
        return f"{r_num}", ""
    else:
        return f"{r_num}/{r_den}", ""

running = True
while running:
    screen.fill(WHITE)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # Mouse click to select input box
        if event.type == pygame.MOUSEBUTTONDOWN:
            mx, my = event.pos
            if 120 <= mx <= 260 and 100 <= my <= 150:
                active_input = "num"
            elif 340 <= mx <= 480 and 100 <= my <= 150:
                active_input = "den"
            else:
                active_input = None

        # Keyboard input
        if event.type == pygame.KEYDOWN:
            if active_input:
                if event.key == pygame.K_BACKSPACE:
                    if active_input == "num":
                        numerator_text = numerator_text[:-1]
                    else:
                        denominator_text = denominator_text[:-1]
                elif event.key == pygame.K_RETURN:
                    try:
                        n = int(numerator_text)
                        d = int(denominator_text)
                        res, err = reduce_fraction(n, d)
                        result_text = res
                        error_text = err
                    except ValueError:
                        error_text = "Please enter integers!"
                        result_text = ""
                else:
                    char = event.unicode
                    if char in "0123456789-":
                        if active_input == "num":
                            numerator_text += char
                        else:
                            denominator_text += char

    # Title
    title_surf = font.render("Fraction Reducer", True, BLACK)
    screen.blit(title_surf, (220, 30))

    # Numerator input box
    pygame.draw.rect(screen, LIGHT_GRAY if active_input == "num" else GRAY, (120, 100, 140, 50), 2)
    num_surf = font.render(numerator_text, True, BLACK)
    screen.blit(num_surf, (130, 110))
    label_num = small_font.render("Numerator", True, BLACK)
    screen.blit(label_num, (120, 160))

    # Fraction slash
    slash_surf = font.render("/", True, BLACK)
    screen.blit(slash_surf, (285, 110))

    # Denominator input box
    pygame.draw.rect(screen, LIGHT_GRAY if active_input == "den" else GRAY, (340, 100, 140, 50), 2)
    den_surf = font.render(denominator_text, True, BLACK)
    screen.blit(den_surf, (350, 110))
    label_den = small_font.render("Denominator", True, BLACK)
    screen.blit(label_den, (340, 160))

    # Equal sign and result
    eq_surf = font.render("=", True, BLACK)
    screen.blit(eq_surf, (510, 110))
    result_surf = font.render(result_text, True, BLUE)
    screen.blit(result_surf, (530, 110))

    # Error message
    if error_text:
        err_surf = small_font.render(error_text, True, RED)
        screen.blit(err_surf, (150, 220))

    # Bottom hint
    info_text = small_font.render("Hint: Input numerator & denominator, press Enter to reduce", True, (80,80,80))
    screen.blit(info_text, (120, 280))

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

pygame.quit()
