import pygame
from entropy_pool import load_pool as load_entropy_pool

def run():
    pool = load_entropy_pool()
    if not pool:
        print("⚠️ No entropy data found!")
        return

    pygame.init()
    WIDTH, HEIGHT = 800, 600
    screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
    pygame.display.set_caption("Quantum Heatmap Visualizer")

    clock = pygame.time.Clock()
    pixel_size = 10  # Size of each grid cell
    pool_index = 0

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.VIDEORESIZE:
                WIDTH, HEIGHT = event.size
                screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)

        screen.fill((0, 0, 0))  # Clear screen

        # Draw heatmap grid
        for y in range(0, HEIGHT, pixel_size):
            for x in range(0, WIDTH, pixel_size):
                value = pool[pool_index % len(pool)]
                # Map entropy to heatmap colors (blue → green → yellow → red)
                color = entropy_to_heat_color(value)
                pygame.draw.rect(screen, color, (x, y, pixel_size, pixel_size))
                pool_index += 1

        pygame.display.flip()
        clock.tick(10)  # Slow-ish update for visual clarity

    pygame.quit()

def entropy_to_heat_color(value):
    """Map entropy value (0-255) to heatmap colors (blue → green → yellow → red)."""
    if value < 64:
        return (0, 0, int(255 * (value / 64)))  # Blue shades
    elif value < 128:
        return (0, int(255 * ((value - 64) / 64)), 255)  # Cyan to Green
    elif value < 192:
        return (int(255 * ((value - 128) / 64)), 255, 0)  # Green to Yellow
    else:
        return (255, int(255 * (1 - ((value - 192) / 64))), 0)  # Yellow to Red
