# make art
import board, digitalio, adafruit_lis3dh, busio, time, neopixel, random
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile
from adafruit_led_animation.animation.blink import Blink
from adafruit_led_animation.animation.chase import Chase
from adafruit_led_animation.animation.comet import Comet
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.animation.sparkle import Sparkle
from adafruit_led_animation.animation.SparklePulse import SparklePulse
from adafruit_led_animation.animation.solid import Solid


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))
)

strip_num_of_lights = 50


#set up light strip
strip = neopixel.NeoPixel(board.A2, strip_num_of_lights, brightness = 0.5, auto_write=True)

# Set up accelerometer
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

# Read, then plot the three axes x, y, z
x, y, z = accelerometer.acceleration

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

# set path to the folder where the sound files can be found on the device
#need to get sounds

#create list of affirmations
affirmations = ["I_am_brave.wav", "Face_the_world_with_confidence.wav", "Accomplish_my_goals.wav", "happy.wav", "i_am_thankful.wav", "stronger.wav", "wishes.wav"]

path = "affirmations/"


#make animations
chase_strip = Chase(strip, speed=0.1, color=WHITE, size=1, spacing=1)
comet_strip = Comet(strip, speed=0.05, color=WHITE, tail_length = int(strip_num_of_lights/4), bounce=True)
pulse_strip = Pulse(strip, speed=0.05, color=WHITE, period=2)
sparkle_strip = Sparkle(strip, speed=0.05, color=WHITE)
sparkle_pulse_strip = SparklePulse(strip, speed=0.05, period=5, color=WHITE)

chase_strip.animate()
comet_strip.animate()
pulse_strip.animate()
sparkle_strip.animate()
#sparkle_pulse_strip.animate()

animations = [chase_strip, comet_strip, pulse_strip, sparkle_strip, sparkle_pulse_strip]

# Call this function, passing in a string (filename), with the extension (.wav)
#animate and play affirmation
def play_sound(filename, animations):
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        animation = random.choice(animations)
        animations.remove(animation)
        while audio.playing:
            #light up random lights
            animation.animate()
    solid = Solid(strip, color=BLACK)
    solid.animate()
    return animations

old_sign = "negative"

while True:

    x, y, z = accelerometer.acceleration
    print("y", y)
    time.sleep(.2)

    #reset either list if it gets empty
    if len(affirmations) == 0:
        affirmations = ["I_am_brave.wav", "Face_the_world_with_confidence.wav", "Accomplish_my_goals.wav", "happy.wav", "i_am_thankful.wav", "stronger.wav", "wishes.wav"]
       #if we went thru all of them then start over
    if len(animations) == 0:
        animations = [chase_strip, comet_strip, pulse_strip, sparkle_strip, sparkle_pulse_strip]

    if y > 1:
        sign = "positive"
    else:
        sign = "negative"

    #if there was a flip, call play sound
    if abs(y) > 8 and sign != old_sign:
        if y > 1:
            old_sign = "positive"
        else:
            old_sign = "negative"
        check = y
        choose = random.choice(affirmations)
        animations = play_sound(choose, animations)
        affirmations.remove(choose)



