# Caitlyn Yee
# Sorting Hat Final Project
import board, time, digitalio, adafruit_lis3dh, busio, random, neopixel
from adafruit_debouncer import Button
from adafruit_led_animation.animation.sparkle import Sparkle
from adafruit_led_animation.animation.chase import Chase
from adafruit_led_animation.animation.rainbowchase import RainbowChase
from adafruit_led_animation.animation.pulse import Pulse

# set up neopixel and neopixel strip
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness = 0.5, auto_write=True)
led_pin = board.A6
num_leds = 20
strand = neopixel.NeoPixel(led_pin, num_leds, brightness=0.85, auto_write=False)

# set up button to start program
#button_A = digitalio.DigitalInOut(board.BUTTON_A)
#button_A.switch_to_input(pull = digitalio.Pull.DOWN)
button_A_input = digitalio.DigitalInOut(board.BUTTON_A)
button_A_input.switch_to_input(digitalio.Pull.DOWN) # Note: Pull.UP for external buttons
button_A = Button(button_A_input, value_when_pressed = True) # NOTE: False for external buttons

# import lines needed to play sound files
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile

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

# set path where sound files can be found
path = "sounds/"

i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
accelerometer = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19, int1=int1)
accelerometer.range = adafruit_lis3dh.RANGE_8_G

print("Sleeping...")
# wait 10 seconds after turning on switch to give you time
# to tape the base on the build without it going off.
print("... and I'm awake!")

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

sparkle = Sparkle(strand, speed=0.1, color=WHITE, num_sparkles=10)
chase = Chase(strand, speed=0.1, size=3, spacing=6, color=WHITE)
pulse = Pulse(pixels, speed=0.1, period=3, color=WHITE)
rainbow_chase = RainbowChase(strand, speed=0.1, size=3, spacing=2, step=8)

sorted_house = ""

def play_sound(filename):
    global sorted_house
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            if(filename == "intro.wav"):
                rainbow_chase.animate()
            elif sorted_house == "gryffindor":
                strand.fill(RED)
                strand.show()
                time.sleep(2)
                break
            elif sorted_house == "hufflepuff":
                strand.fill(YELLOW)
                strand.show()
                time.sleep(2)
                break
            elif sorted_house == "slytherin":
                strand.fill(GREEN)
                strand.show()
                time.sleep(2)
                break
            elif sorted_house == "ravenclaw":
                strand.fill(BLUE)
                strand.show()
                time.sleep(2)
                break

while True:
    # user picks up the hat
    button_A.update() # Gets current state of button
    print(button_A.pressed)
    if button_A.pressed: # True if pressed
        time.sleep(3)
        x, y, z = accelerometer.acceleration
        print((x, y, z))
        houses =  ["gryffindor", "hufflepuff", "slytherin", "ravenclaw"]
        stall =  ["thinking1", "thinking2", "thinking3", "thinking4"]
        play_sound("intro.wav")
        time.sleep(0.1)

        chosen_stall = random.choice(stall)
        play_sound(f"{chosen_stall}.wav"
        time.sleep(0.1)

        sorted_house = random.choice(houses)
        play_sound(f"{sorted_house}.wav")
        time.sleep(4)

        strand.fill((0,0,0))
        strand.show()
            #break
