import time
import board
import neopixel
import adafruit_rgbled
import simpleio

#import code for the LED light ring
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, ORANGE, BLACK, BLUE, RED, PURPLE, YELLOW, TEAL, WHITE
from adafruit_led_animation.animation.solid import Solid

#import code for buttons
from digitalio import DigitalInOut, Direction, Pull

#debounce buttons
from adafruit_debouncer import Debouncer

#color picker button
btn = DigitalInOut(board.D12)
btn.direction = Direction.INPUT
btn.pull = Pull.UP
switch = Debouncer(btn, interval=0.0000001)
button_count = 0

#Launch button
button = DigitalInOut(board.D10)
button.direction = Direction.INPUT
button.pull = Pull.UP
switch = Debouncer (button, interval=0.05)

# Color picker button LED
RED_LED = board.D7
GREEN_LED = board.D13
BLUE_LED = board.D9

# color picker button LED object
led = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
led.color = (0, 0, 0)

#neopixel ring
pixel_pin = board.D5
pixel_num = 24
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=.1, auto_write=False)

#neopixel ring colors
solid = Solid(pixels, color=BLACK)
color1 = Solid(pixels, color=BLUE)
color2 = Solid(pixels, color=RED)
color3 = Solid(pixels, color=PURPLE)
color4 = Solid(pixels, color=TEAL)
color5 = Solid(pixels, color=YELLOW)
color6 = Solid(pixels, color=WHITE)

#neopixel ring code
sparkle_pulse = SparklePulse(pixels, speed=0.25, period=60, color=AMBER)
sparkle = Sparkle(pixels, speed=0.031, color=ORANGE, num_sparkles=200)
animations = AnimationSequence(
    sparkle_pulse, sparkle, advance_interval=6, auto_clear=True,
    )
off = AnimationSequence(solid, auto_clear=True)
blue = AnimationSequence(color1, auto_clear=True)
red = AnimationSequence(color2, auto_clear=True)
purple = AnimationSequence(color3, auto_clear=True)
teal = AnimationSequence(color4, auto_clear=True)
yellow = AnimationSequence(color5, auto_clear=True)
white = AnimationSequence(color6, auto_clear=True)

while True:
    if not btn.value:
        button_count += 1
        if button_count == 1:
            print("0")
            off.animate()
            time.sleep(1)
        if button_count == 2:
            print("1")
            blue.animate()
            time.sleep(1)
        elif button_count == 3:
            red.animate()
            print("2")
            time.sleep(1)
        elif button_count == 4:
            purple.animate()
            print("3")
            time.sleep(1)
        elif button_count == 5:
            teal.animate()
            print("4")
            time.sleep(1)
        elif button_count == 6:
            yellow.animate()
            print("5")
            time.sleep(1)
        elif button_count == 7:
            white.animate()
            print("6")
            time.sleep(1)
        elif button_count >= 8:
            off.animate()
            button_count = 0
    elif button.value:
        animations.animate()
        button_count = 1
        if not button.value:
            led.color = (255,0,255)
            off.animate()