import uasyncio
from machine import Pin
from neopixel import NeoPixel

# Constants
NEOPIXEL_PIN = 26


class LightbarController:
    """
    Controls the LEDs using the NeoPixel library.
    """
    neopixel_count: int
    neopixel: NeoPixel

    def __init__(self, *, neopixel_count: int):
        print('[LightbarController] Initializing Neopixels')
        self.neopixel_count = neopixel_count
        self.neopixel = NeoPixel(Pin(NEOPIXEL_PIN), neopixel_count)

    def start(self):
        uasyncio.create_task(self.run_loop())

    async def run_loop(self):
        print('[LightbarController] Turning on Neopixels')
        np = self.neopixel
        np.fill((0, 0, 0))

        color_set = [
            (255, 0, 0),
            (255, 255, 0),
            (0, 255, 0),
            (0, 255, 255),
            (0, 0, 255),
            (255, 0, 255),
        ]

        while True:
            for i in range(self.neopixel_count):
                np[i] = color_set[i % len(color_set)]
            np.write()

            await uasyncio.sleep(0.5)
