from machine import Pin
from neopixel import NeoPixel
import math
from time import sleep
import json

config = {
    "state": 0,
    "address": 0,
    "ledBrightness": 1,
    "numLeds": 2,
    "dataPin": 15,
    "targetIP": "127.0.0.1",
    "targetPort": 5005,
    "colours": {
        "WHITE": [255,255,255],
        "BLACK": [0,0,0],
        "RED": [255,0,0],
        "GREEN": [0,255,0],
        "AMBER": [255,255,0],
        "BLUE": [0,0,255],
        "PURPLE": [150,0,255],
        "PINK": [252,3,157]
    }
}

class TSLUMD:

    def __init__(self,config,pixels=None):
        self._config = config
        self.pixels = pixels
        self.init()

    def setColour(self, colour):
        return tuple(math.ceil(i * self._config["ledBrightness"]) for i in self._config["colours"][colour])

    def setLEDS(self, colour):
        if len(colour) > 1:
            for i in range(self._config["numLeds"]):
                self.pixels[i] = self.setColour(colour[i])
        else:
            for i in range(self._config["numLeds"]):
                self.pixels[i] = self.setColour(colour[0])
        self.pixels.write()

    def updateState(self, state):
        if state == 0:                  # is not program, is not preview
            self.setLEDS(["BLACK"])
            return
        elif state == 1:                # is program, is not preview OR is program, is preview
            self.setLEDS(["RED"])
            return
        elif state == 2:                # is not program, is preview
            self.setLEDS(["BLACK", "GREEN"])
            return
        elif state == 3:                # is program with graphics/supersource, is not preview
            self.setLEDS(["RED", "AMBER"])
            return
        elif state == 4:                # is not connected to wifi
            self.setLEDS(["BLACK", "PURPLE"])
            return
        elif state == 5:                # is not connected to Sofie
            self.setLEDS(["BLACK", "PINK"])
            return
        else:                           # not vaild state
            self.setLEDS(["BLACK", "WHITE"])
            return

    def init(self):
        self.pixels = NeoPixel(Pin(config["dataPin"]), config["numLeds"])
        # sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        # sock.bind((self._config["targetIP"], self._config["targetPort"]))
    
    def run(self):
        for i in range(6):
            self._config["state"] = i
            self.updateState(self._config["state"])
            print(i)
            sleep(2)
        # data, addr = sock.recvfrom(18)
        # header = list(data[1:])[0]-128
        # control = list(data[1:2])[0]
        # if self.config["address"] == header:
        #     self.updateState(control)


def getJSON(filename):
    with open(filename+'.json', 'r') as f:
        return json.load(f)

def setJSON(dict, filename):
    with open(filename+'.json', 'w') as f:
        json.dump(dict, f)

# config = getJSON(config,'config')

tslumd = TSLUMD(config)

while True:
    tslumd.run()

    sleep(0.1)