# Write your code here :-)
import board, neopixel, time, random, digitalio
from adafruit_debouncer import Debouncer
from adafruit_led_animation.animation.chase import Chase
from audiocore import WaveFile

try:
    from audioio import AudioOut
except ImportError:
    try:
        from audiopwmio import PWMAudioOut as AudioOut
    except ImportError:
        print("this board does not support audioout")
        pass

RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
WHITE = (255,255,255)

#Change this to whichever spot on your board you've attached your LED strip to
strip_pin = board.D2
strip_num_of_lights = 30
strip = neopixel.NeoPixel(strip_pin, strip_num_of_lights, brightness = 1, auto_write = True)


path = "holiday_sounds/"
#Change this ^^^^ to the title of your folder
audio = AudioOut(board.D3)
#Change this to whichever spot you've attached your audio signal
choices = ["frosty_the_snowman.wav", "santa_claus_is_coming_to_town.wav"]
#Change these to whatever you've named your projects

red_chase = Chase(strip, speed=.1, color=RED, size=1, spacing=1)
green_chase = Chase(strip, speed=.1, color=GREEN, size=1, spacing=1)
blue_chase = Chase(strip, speed=.1, color=BLUE, size=1, spacing=1)
white_chase = Chase(strip, speed=.1, color=WHITE, size=1, spacing=1)
black_chase = Chase(strip, speed=.1, color=BLACK, size=1, spacing=1)

button = digitalio.DigitalInOut(board.D5)
#Change this ^^^ to whichever spot you've attached your button to
button.switch_to_input(pull=digitalio.Pull.UP)


def play_merry_christmas(filename):
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            red_chase.animate()
            time.sleep(.1)
            green_chase.animate()
            time.sleep(.1)
            black_chase.animate()
            pass

def play_frosty_the_snowman(filename):
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            white_chase.animate()
            time.sleep(.1)
            blue_chase.animate()
            time.sleep(.1)
            black_chase.animate()
            pass

while True:
    if button.value == False:
        while button.value == False:
            pass
        print("button pressed")
        current_song = random.choice(choices)
        print("song picked: "+ current_song)
        time.sleep(1)
        if current_song == choices[1]:
            play_merry_christmas(current_song)
        if current_song == choices[0]:
            play_frosty_the_snowman(current_song)
        strip.fill(BLACK)
        time.sleep(1)


