import board, time, neopixel, pwmio, digitalio, analogio, math, touchio
from audiopwmio import PWMAudioOut as AudioOut
from adafruit_motor import servo
from audiocore import WaveFile
from adafruit_led_animation.animation.chase import Chase
from adafruit_led_animation.sequence import AnimationSequence


#Import colors for the led light strip
from adafruit_led_animation.color import (
    AMBER, #(255, 100, 0)
    BLACK, #OFF (0, 0, 0)
    GOLD, # (255, 222, 30)
    JADE, # (0, 255, 40)
    MAGENTA, #(255, 0, 20)
    ORANGE, # (255, 40, 0)
    PINK, # (242, 90, 255)
    PURPLE, # (180, 0, 255)
    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))
)

INDIGO = (63,0,255)
VIOLET = (127,0,255)

#List of colors for led lights to loop through while audio is playing
colors = [AMBER, JADE, MAGENTA,ORANGE, PINK, PURPLE, VIOLET, INDIGO]

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

#Path for music sound
path = "music-sounds/"

#Setting up capacitive touch
pad = [board.A4, board.A5]
touchpad =[]

for i in range(len(pad)):
    touchpad.append(touchio.TouchIn(pad[i]))


#Setting up the neopixel strip
strip_num_of_lights = 30
strip_pin = board.A2
strip = neopixel.NeoPixel(strip_pin, strip_num_of_lights, brightness=1.0, auto_write=True)

#Set up servo and calibrate it
pwm = pwmio.PWMOut(board.A3, frequency=50)
servo_1 = servo.Servo(pwm, min_pulse = 700, max_pulse = 2450)
servo_1.angle = 0
#Function to move ballerina
# def dance():
#     if servo_1.angle == 0:
#         for i in range(0,185,5):
#             servo_1.angle = i
#             time.sleep(0.1)
#     else:
#         for i in range(180,-5,-5):
#             servo_1.angle=i
#             time.sleep(0.1)

#Set up animation for leds, Chase animation but with alternating colors
def animation(colors):
    increasing = True
    factor = int(strip_num_of_lights/len(colors))
    for i in range(len(colors)):
        for j in range(0, len(colors)+1, 2):
            for z in range(factor+1):
                strip[(j * factor) + z] = colors[i]

            #to move the dancer, by increments of 20% or 36 points each time
            if increasing:
                value = math.floor(servo_1.angle + 36)
                if value >= 180:
                    increasing = False
                    servo_1.angle = 180
                else:
                    servo_1.angle = value

            if not increasing:
                value = math.floor(servo_1.angle - 36)
                if value <= 0:
                    increasing = True
                    servo_1.angle = 0
                else:
                    servo_1.angle = value

            time.sleep(0.34)
            strip.fill(BLACK)

#Function to play a sound
def play_sound(filename: string):
    with open(path + filename,"rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            animation(colors)


playing=True

while playing:
    #Ballerina, music, and led light strip are enabled by a clap
    if touchpad[0].value:
        strip.brightness = 1.0
        play_sound("ballerina.wav")
    else:
        strip.fill(BLACK)

    if touchpad[1].value:
        strip.brightness = 0.0
        play_sound("ballerina.wav")

