import board
import neopixel
import time
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile

# imports needed for bluetooth
from digitalio import DigitalInOut, Direction, Pull
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.color_packet import ColorPacket
from adafruit_bluefruit_connect.button_packet import ButtonPacket

# import animations and colors
import adafruit_led_animation
from adafruit_led_animation.animation.solid import Solid
#from adafruit_led_animation.animation.colorcycle import ColorCycle
#from adafruit_led_animation.animation.blink import Blink
from adafruit_led_animation.animation.comet import Comet
#from adafruit_led_animation.animation.chase import Chase
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.animation.rainbow import Rainbow
from adafruit_led_animation.animation.rainbowChase import RainbowChase
from adafruit_led_animation.animation.rainbowcomet import RainbowComet
from adafruit_led_animation.animation.rainbowsparkle import RainbowSparkle
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.sequence import AnimateOnce
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))
)

# setup bluetooth
ble = BLERadio()
uart_server = UARTService()
advertisement = ProvideServicesAdvertisement(uart_server)
# CPB name so that it shows up on the bluefruit app
advertisement.complete_name = "DaftPunk"

runAnimation = False
animation_number = -1
lightPosition = -1

# Update to match the pin connected to your NeoPixels
led_pin = board.A1
# UPDATE NUMBER BELOW to match the number of NeoPixels you have connected
num_leds = 20
# color default
defaultColor = WHITE
pickedColor = defaultColor

# timing for animations
defaultTime = 0.2
minWaitTime = 0.01
hundredths = 0.01
tenths = 0.1
adjustedTime = defaultTime

# neopixel set up -- only need 1 because you are connecting all your LED strips together with the soldering
strip = neopixel.NeoPixel(led_pin, num_leds, brightness=1.00, auto_write=False)

# Comet animation with dimming tail
cometTailLength = int(num_leds/3) + 1

# Set LEDs to light up sold in the default color
strip.fill(pickedColor)
strip.write()

print("first")
def runSelectedAnimation():
    if animation_number == 1:
        # larson()
        print("comet")
        cometAnimation = Comet(strip, speed=adjustedTime, color=pickedColor, tail_length=cometTailLength, bounce=False)
        animations = AnimateOnce(cometAnimation)
        while animations.animate():
            pass
    elif animation_number == 2:
        print("pulse")
        pulseAnimation = Pulse(strip, speed=adjustedTime, color=pickedColor, period=3)
        animations = AnimateOnce(pulseAnimation)
        while animations.animate():
            pass
    elif animation_number == 3:
        print("rainbow sparkes")
        sparklePulseAnimation = rainbowSparkleAnimation(strip, speed=adjustedTime, period=12, color=pickedColor)
        animations = AnimateOnce(sparklePulseAnimation)
        while animations.animate():
            pass
    elif animation_number == 4:
        print("rainbowChase")
        rainbowAnimation = Rainbow(strip, speed=adjustedTime, period=2)
        rainbowchaseAnimation = rainbowChase(strip, speed=adjustedTime, period=5, num_sparkles=int(num_leds/3))
        rainbowCometAnimation = rainbowChase(strip, speed=adjustedTime, tail_length=cometTailLength, bounce=True)
        animations = AnimateOnce(rainbowAnimation, rainbowCometAnimation, rainbowSparkleAnimation)
        while animations.animate():
            pass


while True:
    ble.start_advertising(advertisement)
    while not ble.connected:
        if runAnimation == True:
            runSelectedAnimation()
            print("not connected")
    ble.stop_advertising()
    print("connected")

    while ble.connected:
        if uart_server.in_waiting:
            try:
                packet = Packet.from_stream(uart_server)
            except ValueError:
                continue

            if isinstance(packet, ColorPacket):
                print("sent")
                print("pickedColor = ", ColorPacket)
                runAnimation = False
                animation_number = 0
                strip.fill(packet.color)
                strip.write()
                pickedColor = packet.color
                fade_color = (pickedColor[0]//2, pickedColor[1]//2, pickedColor[2]//2)
                light_position = -1

            if isinstance(packet, ButtonPacket):
                if packet.pressed:
                    if packet.button == ButtonPacket.BUTTON_1:
                        animation_number = 1
                        runAnimation = True
                    elif packet.button == ButtonPacket.BUTTON_2:
                        animation_number = 2
                        runAnimation = True
                    elif packet.button == ButtonPacket.BUTTON_3:
                        animation_number = 3
                        runAnimation = True
                    elif packet.button == ButtonPacket.BUTTON_4:
                        animation_number = 4
                        runAnimation = True

        if runAnimation == True:
            runSelectedAnimation()


