import board, neopixel, time, random, digitalio, touchio
from analogio import AnalogIn
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile

from adafruit_led_animation.color import (
    BLACK, #OFF (0, 0, 0)
    GREEN, # (0, 255, 0)
    RED, # (255, 0, 0)
    WHITE, # (255, 255, 255)
)

strip_num_of_lights = 30
strip_pin = board.A1
strip = neopixel.NeoPixel(strip_pin, strip_num_of_lights, brightness=1.0, auto_write=True)

potentiometer= AnalogIn(board.A2)

speaker = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker.direction = digitalio.Direction.OUTPUT
speaker.value = True
audio = AudioOut(board.SPEAKER)

path = "christmas/"
sounds = ["elf.wav","santasworkshop.wav", "santa.wav","underthetree.wav"]

colors = [BLACK, GREEN, RED, WHITE]

old = -1
oldSound = -1

def animation_index(old):
    index = random.randint(0,4)
    while index == old:
        index = random.randint(0,4)
    return index

def sound_index(oldSound):
    index = random.randint(0,len(sounds)-1)
    while index == oldSound:
        index = random.randint(0,len(sounds)-1)
    return index

def brightness_adjustment():
    reading = potentiometer.value
    stripBrightness = potentiometer.value * (1 / 65520)
    strip.brightness = stripBrightness




def play_file(filename: string, index: int):
    with open(path + filename,"rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            brightness_adjustment()

            #animations
            if index == 0:
                #sparkle
                i = random.randint(0,29)
                j = random.randint(0,len(colors)-1)
                strip[i] = colors[j]
            elif index == 1:
                #chase
                for i in range(28):
                    strip[i] = GREEN
                    strip[i+1] = GREEN
                    strip[i+2] = GREEN
                    time.sleep(0.05)
                    strip[i] = BLACK
                    strip[i+1] = BLACK
                    strip[i+2] = BLACK
                    brightness_adjustment()
            elif index == 2:
                #light up, light down
                for i in range(30):
                    strip[i] = RED
                    time.sleep(0.05)
                    brightness_adjustment()
                for i in range(29,-1,-1):
                    strip[i] = BLACK
                    time.sleep(0.05)
                    brightness_adjustment()
            elif index == 3:
                #flash
                j = random.randint(0,len(colors)-1)
                strip.fill(j)
                time.sleep(0.1)
                strip.fill(BLACK)
                brightness_adjustment()
            else:
                #green to red change
                strip.fill(RED)
                time.sleep(0.8)
                strip.fill(GREEN)
                time.sleep(0.8)
                strip.fill(WHITE)
                time.sleep(0.8)
                brightness_adjustment()


start = touchio.TouchIn(board.A3)

while True:
    strip.fill(GREEN)
    brightness_adjustment()

    if start.value:
        strip.fill(BLACK)
        newIndex = animation_index(old)
        old = newIndex

        soundIndex = sound_index(oldSound)
        oldSound = soundIndex

        play_file(sounds[soundIndex], newIndex)
        print("Reached")




