import board
import neopixel
import time
import digitalio
import random
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile
import adafruit_vl53l1x
from rainbowio import colorwheel
import analogio
import adafruit_lis3dh
import busio

i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
accelerometer = adafruit_lis3dh.LIS3DH_I2C(i2c, address =0x19, int1=int1)
accelerometer.range = adafruit_lis3dh.RANGE_8_G

strip_pin = board.A1
strip_number_of_lights = 20
light_brightness = 1

strip = neopixel.NeoPixel(strip_pin, strip_number_of_lights, brightness = light_brightness, auto_write = True)
strip.fill((0,0,0))

i2c = board.I2C()
distance_sensor = adafruit_vl53l1x.VL53L1X(i2c)
distance_sensor.distance_mode = 1
distance_sensor.timing_budget = 100

distance_sensor.start_ranging() # start the sensor

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

path = "sounds/"
def play_file(filename):
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            random_color = random.randint(0,255)
            strip.fill(colorwheel(random_color))
            time.sleep(0.01)

proximity_threshold = 70

mode = 1

DARK_ORANGE = (247, 95, 28)
LIGHT_ORANGE = (255, 154, 0)
PURPLE = (136, 30, 228)
NEON_GREEN = (133,226,31)
RED = (255,0,0)

halloween_colors = [DARK_ORANGE,LIGHT_ORANGE,PURPLE,NEON_GREEN]

strip.fill((0,0,0))
solid = "off"
while True:

    if distance_sensor.data_ready:
        distance = distance_sensor.distance
        if type(distance) != float:
            distance = 1000
        print(f'distance: {distance}, mode: {mode}')

        if accelerometer.shake(shake_threshold = 12):
            solid = "off"
            mode += 1
            if mode > 3:
                mode = 1
            for i in range(3):
                strip.fill(RED)
                time.sleep(0.5)
                strip.fill((0,0,0))
                time.sleep(0.5)

        time.sleep(0.1)

        if distance <= proximity_threshold and mode == 3:
            play_file('bones_cursed.wav')
            strip.fill((0,0,0))

        elif distance <= proximity_threshold and mode == 2:
            while distance <= proximity_threshold:
                color_value = round((distance/proximity_threshold)*256)
                strip.fill(colorwheel(color_value))
                distance_sensor.clear_interrupt()
                distance = distance_sensor.distance
                if type(distance) != float:
                    distance = 1000
            strip.fill((0,0,0))

        elif mode == 1 and solid == "off":
            strip.fill(random.choice(halloween_colors))
            solid = "on"

        distance_sensor.clear_interrupt() # clear out the reading for next use



