import board, time, digitalio, touchio, 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.colorcycle import ColorCycle
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.rainbow import Rainbow
from adafruit_led_animation.animation.rainbowchase import RainbowChase
from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation.sequence import AnimationSequence

from adafruit_led_animation.color import (
    AMBER, #(255, 100, 0)
    AQUA, # (0, 254, 252)
    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)

colors = [RED, BLACK]

strip_pin = board.A1
strip_num_of_lights = 30
strip = neopixel.NeoPixel(strip_pin, strip_num_of_lights, brightness = 0.5, auto_write=True)

blink_strip = Blink(strip, speed=0.5, color=RED)
colorcycle_strip = ColorCycle(strip, 0.1, colors=colors)
chase_strip = Chase(strip, speed=0.1, color=RED, size=3, spacing=6)
comet_strip = Comet(strip, speed=0.05, color=RED, tail_length=int(strip_num_of_lights/4), bounce=True)
pulse_strip = Pulse(strip, speed=0.05, color=RED, period=2)
sparkle_strip = Sparkle(strip, speed=0.05, color=RED)
sparkle_pulse_strip = SparklePulse(strip, speed=0.05, period=5, color=RED)
solid_strip = Solid(strip, color=BLACK)

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

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

def animation(last):
    index = random.randint(0,6)
    while index == last:
        index = random.randint(0,6)
    return index


def play_sound(filename:string, index: int):
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            animations[index].animate()

path = "albania/"
albania_sounds = ["FIERI.wav", "KORCA.wav","KUKSI.wav","SHKODRA.wav","TIRANA.wav","VLORA.wav"]

touch_A2 = touchio.TouchIn(board.A2)
touch_A3 = touchio.TouchIn(board.A3)
touch_A4 = touchio.TouchIn(board.A4)
touch_A5 = touchio.TouchIn(board.A5)
touch_A6 = touchio.TouchIn(board.A6)
touch_TX = touchio.TouchIn(board.TX)

last= -1
while True:

    index = animation(last)
    last = index

    if touch_A2.value:
        play_sound(albania_sounds[0], index)
        time.sleep(1.0)
        solid_strip.animate()
    elif touch_A3.value:
        play_sound(albania_sounds[1], index)
        time.sleep(1.0)
        solid_strip.animate()
    elif touch_A4.value:
        play_sound(albania_sounds[2], index)
        time.sleep(1.0)
        solid_strip.animate()
    elif touch_A5.value:
        play_sound(albania_sounds[3], index)
        time.sleep(1.0)
        solid_strip.animate()
    elif touch_A6.value:
        play_sound(albania_sounds[4], index)
        time.sleep(1.0)
        solid_strip.animate()
    elif touch_TX.value:
        play_sound(albania_sounds[5], index)
        time.sleep(1.0)
        solid_strip.animate()








