import board, neopixel, adafruit_mpr121, busio, digitalio, mount_sd, random, time
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile
from adafruit_led_animation.animation.rainbowchase import RainbowChase
from adafruit_led_animation.animation.rainbow import Rainbow
from adafruit_led_animation.animation.blink import Blink
from adafruit_led_animation.animation.comet import Comet
from adafruit_led_animation.animation.chase import Chase
from adafruit_led_animation.sequence import AnimationSequence
from adafruit_led_animation.color import (
    AMBER, #(255, 100, 0)
    AQUA, # (50, 255, 255)
    BLACK, #OFF (0, 0, 0)
    BLUE, # (0, 0, 255)
    CYAN, # (0, 255, 255)
    GOLD, # (255, 222, 30)
    GREEN, # (0, 255, 0)
    JADE, # (0, 255, 40)
    MAGENTA, #(255, 0, 20)
    OLD_LACE, # (253, 245, 230)
    ORANGE, # (255, 40, 0)
    PINK, # (242, 90, 255)
    PURPLE, # (180, 0, 255)
    RED, # (255, 0, 0)
    TEAL, # (0, 255, 120)
    WHITE, # (255, 255, 255)
    YELLOW, # (255, 150, 0)
    RAINBOW # a list of colors to cycle through
    # RAINBOW is RED, ORANGE, YELLOW, GREEN, BLUE, and PURPLE ((255, 0, 0), (255, 40, 0), (255, 150, 0), (0, 255, 0), (0, 0, 255), (180, 0, 255))
)

colors = [AMBER, AQUA,BLUE, # (0, 0, 255)
    CYAN, # (0, 255, 255)
    GOLD, # (255, 222, 30)
    GREEN, # (0, 255, 0)
    JADE, # (0, 255, 40)
    MAGENTA, #(255, 0, 20)
    OLD_LACE, # (253, 245, 230)
    ORANGE, # (255, 40, 0)
    PINK, # (242, 90, 255)
    PURPLE, # (180, 0, 255)
    RED, # (255, 0, 0)
    TEAL, # (0, 255, 120)
    WHITE, # (255, 255, 255)
    YELLOW
]
audio = AudioOut(board.GP16)
letter_path = "/sd/alphabet/"
sound_path = "/sd/sounds/"

i2c0 = busio.I2C(board.GP5, board.GP4)
i2c1 = busio.I2C(board.GP27, board.GP26)
pad1 = adafruit_mpr121.MPR121(i2c0, address=0x5A)
pad2 = adafruit_mpr121.MPR121(i2c0, address=0x5b)
pad3 = adafruit_mpr121.MPR121(i2c1)
strip = neopixel.NeoPixel(board.GP18, 30, brightness=0.5)

letters = ["A.wav", "B.wav", "C.wav", "D.wav", "E.wav", "F.wav", "G.wav",
           "H.wav", "I.wav", "J.wav", "K.wav", "L.wav", "M.wav", "N.wav", "O.wav",
           "P.wav", "Q.wav", "R.wav","S.wav", "T.wav", "U.wav", "V.wav", "W.wav", "X.wav", "Y.wav", "Z.wav"]


# Button-sound mapping
button_sounds = {
    (pad1, 0): "A.wav",
    (pad1, 1): "B.wav",
    (pad1, 2): "C.wav",
    (pad1, 3): "D.wav",
    (pad1, 4): "E.wav",
    (pad1, 5): "F.wav",
    (pad1, 6): "G.wav",
    (pad1, 7): "H.wav",
    (pad1, 8): "I.wav",
    (pad1, 9): "J.wav",
    (pad1, 10): "K.wav",
    (pad1, 11): "L.wav",
    (pad2, 0): "M.wav",
    (pad2, 1): "N.wav",
    (pad2, 2): "O.wav",
    (pad2, 3): "P.wav",
    (pad2, 4): "Q.wav",
    (pad2, 5): "R.wav",
    (pad2, 6): "S.wav",
    (pad2, 7): "T.wav",
    (pad2, 8): "U.wav",
    (pad2, 9): "V.wav",
    (pad2, 10): "W.wav",
    (pad2, 11): "X.wav",
    (pad3, 0): "Y.wav",
    (pad3, 1): "Z.wav"
}
blink = Blink(strip, speed=0.5, color=GREEN)
comet = Comet(strip, speed=0.01, color=PURPLE, tail_length=10, bounce=True)
chase = Chase(strip, speed=0.1, size=3, spacing=6, color=AMBER)
rainbow_chase = RainbowChase(strip, speed=0.01, size=5, spacing=0, step=20)
rainbow = Rainbow(strip, speed=0.1, period=2)
animations = AnimationSequence(rainbow, blink, comet, chase, advance_interval=3, auto_clear=True)

quiz_mode = False
current_letter = None

def play_letter_sound(filename):
    with open(letter_path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        color = random.choice(colors)
        while audio.playing:
            strip.fill(color)
            strip.show()

def play_correct():
    global current_letter, quiz_mode
    with open(sound_path + "correct.wav", "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            while True:
                animations.animate()
                if pad3[2].value:
                    current_letter = None
                    strip.fill(AMBER)
                    strip.show()
                    time.sleep(0.5)
                    break
                elif pad3[3].value:
                    quiz_mode = False
                    strip.fill(BLACK)
                    strip.show()
                    time.sleep(0.5)
                    break
                time.sleep(0.01)

def play_incorrect():
    with open(sound_path+"incorrect.wav", "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            flash_color(RED)

def flash_color(color):
    strip.fill(color)
    strip.show()

def choose_random_letter():
    return random.choice(letters)

def start_quiz():
    global current_letter
    current_letter = choose_random_letter()
    play_letter_sound(current_letter)
    print(f"Quiz Letter: {current_letter}")

def check_answer(pad, index):
    global current_letter
    sound_file = button_sounds.get((pad, index))
    if current_letter == sound_file:
        return True
    else:
        flash_color(RED)
        return False

print("Code running")
strip.fill(BLACK)
strip.show()

# Main loop
while True:
    if pad3[2].value:  # Quiz mode button
        quiz_mode = True
        strip.fill(AMBER)
        strip.show()
        print("Quiz Mode: ON")
        current_letter = None
        time.sleep(0.5)

    elif pad3[3].value:
        quiz_mode = False
        strip.fill(BLACK)
        strip.show()
        print("Quiz Mode: OFF")
        time.sleep(0.5)

    if quiz_mode:
        if current_letter is None:
            start_quiz()

        for (pad, index), sound_file in button_sounds.items():
            if pad[index].value:
                if check_answer(pad, index):
                    play_correct()
                    print("Correct Answer!")
                else:
                    play_incorrect()
                    play_letter_sound(current_letter)
    else:
        for (pad, index), sound_file in button_sounds.items():
            if pad[index].value:
                play_letter_sound(sound_file)
        strip.fill(BLACK)
        strip.show()
