#!/usr/bin/python3
from flask import Flask
from flask_restful import Api, Resource, reqparse

import board
import neopixel

pixels = neopixel.NeoPixel(board.D18, 250, brightness=1.0, auto_write=False, pixel_order=neopixel.GRB)

leds = [
    {
        "name": "all",
        "enabled": False,
        "start": 0,
        "end": 250,
        "r": 255,
        "g": 255,
        "b": 255
    },
    {
        "name": "vagrant",
        "enabled": False,
        "start": 0,
        "end": 34,
        "r": 0,
        "g": 84,
        "b": 251
    },
    {
        "name": "packer",
        "enabled": False,
        "start": 34,
        "end": 68,
        "r": 4,
        "g": 165,
        "b": 255
        },
    {
        "name": "terraform",
        "enabled": False,
        "start": 68,
        "end": 102,
        "r": 92,
        "g": 78,
        "b": 229
    },
    {
        "name": "vault",
        "enabled": False,
        "start": 102,
        "end": 136,
        "r": 106,
        "g": 109,
        "b": 122
    },
    {
        "name": "nomad",
        "enabled": False,
        "start": 136,
        "end": 170,
        "r": 37,
        "g": 186,
        "b": 129
    },
    {
        "name": "consul",
        "enabled": False,
        "start": 170,
        "end": 204,
        "r": 198,
        "g": 42,
        "b": 113
    },
    {
        "name": "hashicorp",
        "enabled": False,
        "start": 204,
        "end": 250,
        "r": 255,
        "g": 255,
        "b": 255
    }
]

class Led(Resource):
    def get(self, name):
        for led in leds:
            if led["name"] == name:
                return led, 200
        return "led not found", 404

    def post(self, name):
        for led in leds:
            if led["name"] == name:
                # Turn on all the leds for the tile.
                for index in range(led["start"], led["end"]):
                    pixels[index] = (led["r"], led["g"], led["b"])
                    # print(led["r"], led["g"], led["b"])
                
                # Send the new color.
                pixels.show()

                # Return new status.
                led["enabled"] = True
                return led, 200
        return "led not found", 404

    def delete(self, name):
        for led in leds:
            if led["name"] == name:
                 # Turn off all the leds for the tile.
                for index in range(led["start"], led["end"]):
                    pixels[index] = (0, 0, 0)
                    # print(0, 0, 0)
                
                # Send the new color.
                pixels.show()

                # Return new status.
                led["enabled"] = False
                return led, 200
        return "led not found", 404

app = Flask(__name__)
api = Api(app)
api.add_resource(Led, "/led/<string:name>")
app.run(debug=True)