import board
import neopixel
import time
import digitalio
from adafruit_debouncer import Button
import random

#colors
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
ORANGE = (255, 40, 0)
PINK = (242, 90, 255)
PURPLE = (180, 0, 255)
YELLOW = (255, 150, 0)

RAINBOW = [RED, BLUE, GREEN, ORANGE, PINK, PURPLE, YELLOW]


# Define the matrix parameters
WIDTH = 16
HEIGHT = 16
PIXEL_COUNT = WIDTH * HEIGHT

# Initialize the NeoPixel matrix
pixels = neopixel.NeoPixel(board.GP15, PIXEL_COUNT, brightness=0.2, auto_write=False)

# Button setup
button_input = digitalio.DigitalInOut(board.GP16)  # Wired to GP16 (change to your actual pin)
button_input.switch_to_input(digitalio.Pull.UP)  # Pull-up for external buttons
button = Button(button_input)  # False for external buttons

# Define the alphabet as mirrored binary grids
MIRRORED_ALPHABET = {
    "A": [
        "0000111111100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000111111100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000000000000000",
        "0000000000000000",
    ],
    "B": [
    "0000111111110000",
    "0001000000010000",
    "0001000000010000",
    "0000100000010000",
    "0000011111110000",
    "0000100000010000",
    "000100000001000",
    "001000000001000",
    "0010000000010000",
    "0001000000010000",
    "0000111111010000",
    "0000000000000000",
    "0000000000000000",
    "0000000000000000",
    "0000000000000000",
    "0000000000000000"
    ],
    "C": [
        "0000011111100000",
        "0000000000010000",
        "0000000000001000",
        "0000000000000100",
        "0000000000000100",
        "0000000000000100",
        "0000000000000100",
        "0000000000000100",
        "0000000000000100",
        "0000000000001000",
        "0000011111100000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "D": [
        "0000000000000000",
        "0000000111111000",
        "0000001000001000",
        "0000010000001000",
        "0000100000001000",
        "0001000000001000",
        "0010000000001000",
        "0010000000001000",
        "0010000000001000",
        "0010000000001000",
        "0010000000001000",
        "0001000000001000",
        "0000100000001000",
        "0000010000001000",
        "0000001111111000",
        "0000000000000000",
    ],
    "E": [
        "0000000000000000",
        "0001111111110000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000001111110000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0001111111110000",
        "0000000000000000",
        "0000000000000000",
    ],
    "F": [
        "0000000000000000",
        "0000000000000000",
        "00011111111100000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000001111110000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "G": [
        "0000000000000000",
        "0001111111000000",
        "0000000000100000",
        "0000000000010000",
        "0000000000001000",
        "0000000000001000",
        "0000000000001000",
        "0000000000001000",
        "0111111000001000",
        "0100000000001000",
        "0100000000001000",
        "0100000000010000",
        "0010000000100000",
        "0001000001000000",
        "00001111100000000",
        "0000000000000000",
    ],
    "H": [
        "0000000000000000",
        "0000000000000000",
        "000000000000000",
        "0000100000010000",
        "0000100000010000",
        "0000100000010000",
        "0000100000010000",
        "0000111111110000",
        "0000100000010000",
        "0000100000010000",
        "0000100000010000",
        "0000100000010000",
        "0000100000010000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "I": [
        "0001111111110000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0001111111110000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "J": [
        "0000000000000000",
        "0000000000000000",
        "0000111111110000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000010",
        "0000000100000010",
        "0000000010000010",
        "0000000001111100",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "K": [
        "0000000000000000",
        "0000000000000000",
        "0000100000010000",
        "0000010000010000",
        "0000001000010000",
        "0000000100010000",
        "0000000010010000",
        "0000000001010000",
        "0000000000110000",
        "0000000001010000",
        "0000000010010000",
        "0000000100010000",
        "0000001000010000",
        "0000010000010000",
        "0000000000000000",
        "0000000000000000",
    ],
    "L": [
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000010000",
        "00000000000100000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000111111110000",
        "0000000000000000",
        "0000000000000000",
    ],
    "M": [
        "0000100000100000",
        "0000110001100000",
        "0000101010100000",
        "0000101010100000",
        "0000100100100000",
        "0000100100100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "N": [
        "0000000000000000",
        "000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0001000000110000",
        "0001000001010000",
        "0001000010010000",
        "0001000100010000",
        "0001001000010000",
        "0001010000010000",
        "0001100000010000",
        "0001000000010000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "O": [
        "0000000000000000",
        "0000011111000000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000011111000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "P": [
        "0000000000000000",
        "0000000000000000",
        "0000000111110000",
        "0000001000010000",
        "0000010000010000",
        "0000100000010000",
        "0000100000010000",
        "0000010000010000",
        "0000001111110000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000010000",
        "0000000000000000",
        "0000000000000000",
    ],
    "Q": [
        "0000000000000000",
        "0000011111000000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000101000100000",
        "0000110000100000",
        "000011111110000",
        "0001000000000000",
        "0010000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "R": [
        "0000000000000000",
        "0000000000000000",
        "0000001111100000",
        "0000010000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000010000100000",
        "0000001111100000",
        "0000000010100000",
        "0000000100100000",
        "0000001000100000",
        "0000010000100000",
        "0000100000100000",
        "0000000000000000",
        "0000000000000000",
    ],
    "S": [
        "0000000000000000",
        "0000000000000000",
        "0000111111111000",
        "0000000000001000",
        "0000000000001000",
        "0000000000001000",
        "0000111111111000",
        "0000100000000000",
        "0000100000000000",
        "0000100000000000",
        "0000100000000000",
        "0000111111111000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "T": [
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0011111111111100",
        "0000000010000000",
        "0000000010000000",
        "0000000010000000",
        "0000000010000000",
        "0000000010000000",
        "0000000010000000",
        "0000000010000000",
        "0000000010000000",
        "0000000010000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "U": [
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000111111100000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "V": [
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0001000000010000",
        "0001000000010000",
        "0000100000100000",
        "0000100000100000",
        "0000010001000000",
        "0000010001000000",
        "0000001010000000",
        "0000001010000000",
        "0000000100000000",
        "0000000100000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "W": [
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100000100000",
        "0000100100100000",
        "0000100100100000",
        "0000101010100000",
        "0000101010100000",
        "0000101010100000",
        "0000101010100000",
        "0000111011100000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "X": [
        "0000100000100000",
        "0000100000100000",
        "0000010001000000",
        "0000010001000000",
        "0000001010000000",
        "0000001010000000",
        "0000000100000000",
        "0000000100000000",
        "0000001010000000",
        "0000001010000000",
        "0000010001000000",
        "0000010001000000",
        "0000100000100000",
        "0000100000100000",
        "0000000000000000",
        "0000000000000000",
    ],
    "Y": [
        "0000100000100000",
        "0000100000100000",
        "0000010001000000",
        "0000010001000000",
        "0000001010000000",
        "0000001010000000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0000000100000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
    "Z": [
        "0000000000000000",
        "0000000000000000",
        "0001111111110000",
        "0000100000000000",
        "0000010000000000",
        "0000001000000000",
        "0000000100000000",
        "0000000010000000",
        "0000000001000000",
        "0000000000100000",
        "0001111111110000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
        "0000000000000000",
    ],
}

def zigzag_index(x, y):
    """Maps (x, y) coordinates to the actual NeoPixel index, accounting for zigzag wiring."""
    if y % 2 == 0:  # Even row (left to right)
        return y * WIDTH + x
    else:  # Odd row (right to left)
        return y * WIDTH + (WIDTH - 1 - x)

def display_letter(letter):
    """Displays a single letter on the NeoPixel matrix."""
    binary_grid = MIRRORED_ALPHABET.get(letter, [])
    for y, row in enumerate(binary_grid):
        for x, pixel in enumerate(row):
            pixel_index = zigzag_index(x, y)
            if pixel == "1":
                pixels[pixel_index] = random.choice(RAINBOW)
            else:
                pixels[pixel_index] = (0, 0, 0)  # Off
    pixels.show()

# Start with the first letter
letters = sorted(list(MIRRORED_ALPHABET.keys()))  # Ensure the letters are in alphabetical order
current_letter_index = 0  # Ensure it starts from the first letter

# Debounce time
last_button_press_time = 0
debounce_delay = 0.2  # 200ms debounce delay

while True:
    # Update the button state
    button.update()

    # Check if enough time has passed since the last button press (debouncing)
    if button.pressed and (time.time() - last_button_press_time) > debounce_delay:
        print("Button Pressed")
        # Move to the next letter
        current_letter_index = (current_letter_index + 1) % len(letters)
        # Update the last button press time
        last_button_press_time = time.time()

    # Display the current letter
    display_letter(letters[current_letter_index])

    time.sleep(0.05)  # Small delay for responsiveness



