import board, time, pwmio, neopixel, touchio, random
#For the rainbow function
from rainbowio import colorwheel
#For the Sparkle function
from adafruit_led_animation.animation.sparkle import Sparkle
#Importing various colors
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))
)

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

#Diwali colors
colors = [RED, YELLOW, MAGENTA, BLUE, ORANGE, GREEN]

#Setup for strand
strip_pin = board.A1
strip_num_of_LEDs = 20
strip = neopixel.NeoPixel(strip_pin, strip_num_of_LEDs, brightness=0.5, auto_write=False)

#Setup for capacitive touch
pad = [board.TX, board.A3]
touchpad = []

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

sparkle_strip = Sparkle(strip, speed=0.05, color=RED, num_sparkles=3)

strip.fill(BLACK)
strip.write()

#Turns on a particular LED strand indecies
def strand_on(list, c):
    for i in list:
        strip[i] = c
        strip.write()

#For touchpad[0]:
#Creates a rainbow pattern that slows down as it reaches the last few lights
def rainbow():
    for i in range(0, strip_num_of_LEDs):
        strip[i] = colorwheel(round(i*255/20))
        strip.write()
        time.sleep(0.01*i)
    return print("Rainbow")

#Sparkles in red
def sparkle_on():
    for i in range(0,30):
        sparkle_strip.animate()
        strip.write()
        time.sleep(0.1)
    strip.fill(BLACK)
    strip.write()
    return print("Sparkle")

#Alternating random colors that flash to mimic fireworks
def fireworks_colors():
    sec = 0
    last_color1 = (0,0,0)
    last_color2 = (0,0,0)
    while sec < 6:
        color1 = random.choice(colors)
        color2 = random.choice(colors)
        while last_color1 == color1:
            color1 = random.choice(colors)
        while last_color2 == color2 or color1 == color2:
            color2 = random.choice(colors)
        strip[10:15] = color1*5
        strip[15:20] = color2*5
        strip.write()
        time.sleep(0.5)
        strip[10:15] = (BLACK)*5
        strip[15:20] = (BLACK)*5
        strip.write()
        time.sleep(0.5)
        sec += 1
    strip.fill(BLACK)
    strip.write()
    time.sleep(1)
    return print("Colors")

#Indian Flag
def flag():
    #BLUE:
    strip[2] = BLUE
    #WHITE:
    strand_on([0,1,3,9,12,16], WHITE)
    #ORANGE:
    strand_on([4,5,10,11,17,18,19], ORANGE)
    #GREEN:
    strand_on([6,7,8,13,14,15], GREEN)
    time.sleep(2)
    print("Flag")
    strip.fill(BLACK)
    strip.write()

#For touchpad[1]:
#Spiral of colors that goes around and back to mimic rangoli
def spiral():
    count = 0
    while count <= len(colors)-2:
        for i in range(strip_num_of_LEDs):
            strip[i] = colors[count]
            strip.write()
            time.sleep(0.1)
        for x in range(strip_num_of_LEDs-1, -1, -1):
            strip[x] = colors[count+1]
            strip.write()
            time.sleep(0.1)
        count += 2
    return print("Spiral")

#Pulsing light to mimic the diya's light
def pulse():
    times = 0
    while times < 5:
        strip.fill(colors[times])
        strip.write()
        for j in range(0,100):
            strip.brightness = j/100
            strip.write()
            time.sleep(0.01)
        for k in range(100,-1,-1):
            strip.brightness = k/100
            strip.write()
            time.sleep(0.01)
        times += 1
    return print("Pulse")

#Trikle down effect with lights in different colors
def waterfall():
    strip.brightness = 1
    layer1 = [17,18,19]
    layer2 = [10,11]
    layer3 = [4,5,12]
    layer4 = [0,3,9]
    layer5 = [1,2,6,16]
    layer6 = [7,8,13,14,15]
    layers = [layer1,layer2,layer3,layer4,layer5,layer6]
    x = 0
    while x < 6:
        colorchoice = colors[x]
        for l in range(len(layers)):
            strip.fill(BLACK)
            strip.write()
            strand_on(layers[l],colorchoice)
            time.sleep(0.15)
        x += 1
    strip.fill(BLACK)
    strip.write()
    return print("Waterfall")

while True:
    if touchpad[0].value:
        #Start of the program
        strip[2] = (255,255,255)
        strip.write()
        time.sleep(1)
        #Functions start
        rainbow()
        sparkle_on()
        fireworks_colors()
        flag()
    if touchpad[1].value:
        spiral()
        pulse()
        waterfall()


