# Working with a NeoPixel LED Light Strip
import board, time, neopixel, pwmio, digitalio, analogio, math, busio, adafruit_lis3dh, random
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile

from adafruit_led_animation.animation.sparkle import Sparkle
from adafruit_led_animation.animation.SparklePulse import SparklePulse
from adafruit_led_animation.sequence import AnimationSequence

from adafruit_led_animation.color import (
    AMBER, #(255, 100, 0)
    AQUA, # (0, 254, 252)
    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))
)


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

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

path = "sounds/"

pixels_pin = board.NEOPIXEL
pixels_num_of_lights = 10
pixels = neopixel.NeoPixel(pixels_pin, pixels_num_of_lights, brightness = 0.5, auto_write=True)

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


last_random = -1
sprakle_on = False

def randomColor():
    color = RED
    random_num = random.randint(0, 1)
    if random_num == 1:
        color = RED
    else:
        color = GREEN
    return color


def sparkle_1(last_random):
    random_number = random.randint(0, 9)
    random_number_strip = random.randint(0,29)
    while last_random == random_number:
        random_number = random.randint(0, 9)
    pixels[random_number] = randomColor()
    strip[random_number_strip] = randomColor()
    time.sleep(0.04)
    last_random = random_number
    return last_random

def play_file(filename: string, state: string, last_random: int):
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            if state == "still":
                pixels.fill(GREEN)
                strip.fill(RED)
            elif state == "shaken":
                last_random = sparkle_1(last_random)


state = "still"
sounds = ["mariah.wav"]
soundIndex = 0

while True:
    state = "still"
    if state == "still":
        play_file("jinglebells1.wav", state, last_random)
    x, y, z = accelerometer.acceleration
    if accelerometer.shake(shake_threshold=10):
        pixels.fill(BLACK)
        state = "shaken"
        play_file(sounds[soundIndex], state, last_random)
        pixels.fill(BLACK)
        if soundIndex < 4:
            soundIndex=0
        else:
            soundIndex += 1


