import pygame
import sys
import os
import select
import time

def get_cmd():
    if select.select([sys.stdin], [], [], 0)[0]:
        return sys.stdin.readline().strip().upper()
    return None

def close_game():
    pygame.quit()
    os._exit(0)

pygame.init()
pygame.mouse.set_visible(False)

# ----------------------------
# SCREEN
# ----------------------------
WIDTH, HEIGHT = 1440, 900
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Control Interface")
clock = pygame.time.Clock()
FPS = 60

# ----------------------------
# COLORS
# ----------------------------
BLACK = (10, 10, 10)
WHITE = (255, 255, 255)
YELLOW = (255, 215, 0)
DARK = (25, 25, 25)
GREEN = (0, 220, 0)
RED = (220, 50, 50)
BLUE = (80, 160, 255)
CYAN = (0, 255, 255)

# ----------------------------
# LAYOUT
# ----------------------------
HUD_HEIGHT = int(HEIGHT * 0.10)
MAIN_HEIGHT = HEIGHT - HUD_HEIGHT

LEFT_WIDTH = int(WIDTH * 0.5)
RIGHT_WIDTH = WIDTH - LEFT_WIDTH

# ----------------------------
# FONT
# ----------------------------
font = pygame.font.SysFont(None, 60)
small_font = pygame.font.SysFont(None, 40)

# ----------------------------
# CLOSE BUTTON
# ----------------------------
close_text = small_font.render("CLOSE", True, WHITE)
close_rect = close_text.get_rect(center=(WIDTH - 80, HUD_HEIGHT // 2))
close_box = close_rect.inflate(40, 30)

# ----------------------------
# HELPERS
# ----------------------------
def draw_circle(x, y, r, color, width=3):
    pygame.draw.circle(screen, color, (x, y), r, width)

def draw_filled_circle(x, y, r, color):
    pygame.draw.circle(screen, color, (x, y), r)

def draw_arrow(x, y, direction, size=35, color=WHITE, filled=False):
    if direction == "UP":
        pts = [(x, y - size), (x - size, y + size), (x + size, y + size)]
    elif direction == "DOWN":
        pts = [(x, y + size), (x - size, y - size), (x + size, y - size)]
    elif direction == "LEFT":
        pts = [(x - size, y), (x + size, y - size), (x + size, y + size)]
    elif direction == "RIGHT":
        pts = [(x + size, y), (x - size, y - size), (x - size, y + size)]
    else:
        return

    pygame.draw.polygon(screen, color, pts, 0 if filled else 3)

# ----------------------------
# POSITIONS
# ----------------------------

# Left arrows
center_x = LEFT_WIDTH // 2
center_y = HUD_HEIGHT + MAIN_HEIGHT // 2
offset = 120

arrows = {
    "UP": (center_x, center_y - offset),
    "DOWN": (center_x, center_y + offset),
    "LEFT": (center_x - offset, center_y),
    "RIGHT": (center_x + offset, center_y),
}

pb_circle = (center_x, center_y)

# Right circles (A-F)
circle_radius = 45

right_start_x = LEFT_WIDTH + RIGHT_WIDTH // 2 - 180
right_start_y = HUD_HEIGHT + MAIN_HEIGHT // 2 - 120
spacing = 160
row_shift = spacing // 2

circles = {
    "A": (right_start_x + 0 * spacing, right_start_y),
    "B": (right_start_x + 1 * spacing, right_start_y),
    "C": (right_start_x + 2 * spacing, right_start_y),

    "D": (right_start_x + 0 * spacing + row_shift, right_start_y + spacing),
    "E": (right_start_x + 1 * spacing + row_shift, right_start_y + spacing),
    "F": (right_start_x + 2 * spacing + row_shift, right_start_y + spacing),
}

# ----------------------------
# ACTIVE STATES (timed pulses)
# ----------------------------
ACTIVE_TIME = 0.3  # seconds
active_until = {}

def activate(key):
    active_until[key] = time.time() + ACTIVE_TIME

def is_active(key):
    return active_until.get(key, 0) > time.time()

# ----------------------------
# MAIN LOOP
# ----------------------------
running = True
while running:
    clock.tick(FPS)

    # cleanup expired
    now = time.time()
    active_until = {k: v for k, v in active_until.items() if v > now}

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

        if event.type == pygame.MOUSEBUTTONDOWN:
            if close_box.collidepoint(pygame.mouse.get_pos()):
                close_game()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                close_game()

    cmd = get_cmd()

    if cmd:
        # EMERGENCY EXIT COMMAND
        if cmd == "RASP":
            running = False

        # arrows
        if cmd in ["UP", "DOWN", "LEFT", "RIGHT"]:
            activate(cmd)

        # center PB
        elif cmd == "PB":
            activate("PB")

        # right grid
        elif cmd in ["A", "B", "C", "D", "E", "F"]:
            activate(cmd)

        elif cmd == "CLOSE":
            close_game()

    # ----------------------------
    # DRAW
    # ----------------------------
    screen.fill(BLACK)

    # HUD
    pygame.draw.rect(screen, DARK, (0, 0, WIDTH, HUD_HEIGHT))
    text = font.render(f"INPUTS CHECK", True, WHITE)
    screen.blit(text, text.get_rect(center=(WIDTH // 2, HUD_HEIGHT // 2)))

    # close
    pygame.draw.rect(screen, WHITE, close_box, 5, border_radius=8)
    screen.blit(close_text, close_rect)

    pygame.draw.line(screen, YELLOW, (0, HUD_HEIGHT), (WIDTH, HUD_HEIGHT), 6)

    # ----------------------------
    # LEFT SIDE (ARROWS + PB)
    # ----------------------------
    for name, pos in arrows.items():
        draw_arrow(
            pos[0],
            pos[1],
            name,
            color=CYAN if is_active(name) else WHITE,
            filled=is_active(name)
        )

    # center PB circle
    draw_filled_circle(
        pb_circle[0],
        pb_circle[1],
        60,
        GREEN if is_active("PB") else DARK
    )
    draw_circle(pb_circle[0], pb_circle[1], 60, WHITE)

    # ----------------------------
    # RIGHT SIDE (A-F circles)
    # ----------------------------
    for key, pos in circles.items():
        color = RED if is_active(key) else BLUE
        if is_active(key):
            draw_filled_circle(pos[0], pos[1], circle_radius, color)
        draw_circle(pos[0], pos[1], circle_radius, WHITE)

    pygame.draw.rect(screen, YELLOW, screen.get_rect(), 8)

    pygame.display.flip()

pygame.quit()
sys.exit()