from enum import IntEnum
from scipy.interpolate import LinearNDInterpolator
import csv
import numpy as np
from kivy.properties import NumericProperty, BooleanProperty
from kivy.event import EventDispatcher
from kivy.graphics import Color

class LightColor:
    def get(val):
        return LightColor.COLORS[val] if val in LightColor.COLORS else [1,1,1,1]
    
    WHITE = 0
    RED = 15
    BLUE = 30
    GREEN = 45
    YELLOW = 60
    MAGENTA = 75
    DARK_BLUE = 90
    ORANGE = 105
    ORANGE_RED = 120
    RAINBOW_FORWARD = 128
    RAINBOW_STOP = 190
    RAINBOW_BACKWARD = 194
    COLORS = {
        WHITE : [1,1,1,1], 
        RED : [0.90, 0.13, 0.16 ,1],
        BLUE : [0.63, 0.85, 0.96 ,1],
        GREEN : [0.00, 0.61, 0.30 ,1],
        YELLOW : [1.00, 0.96, 0.49 ,1],
        MAGENTA : [0.89, 0.00, 0.51 ,1],
        DARK_BLUE : [0.00, 0.56, 0.84 ,1],
        ORANGE : [0.99, 0.81, 0.00 ,1],
        ORANGE_RED : [0.94, 0.52, 0.10 ,1],
              }

class Gobo:
    OPEN = 0
    STAR = 10
    WINDOW = 20
    SPIRAL = 30
    CROSS = 40
    SUN = 50
    SPIRAL_2 = 60
    SPIN = 190



class Light(EventDispatcher):
    tilt = NumericProperty(0)
    pan = NumericProperty(0)
    dimmer = NumericProperty(0)
    focus = NumericProperty(0)
    color = NumericProperty(0)
    shutter = NumericProperty(0)
    gobo = NumericProperty(0)
    gobo_shake = BooleanProperty(False)    
    
    interp: LinearNDInterpolator
    
    def __init__(self, address):
        self.address = address
        #self.color = LightColor.WHITE
        self.interp = None

        self.target_x = 0.5
        self.target_y = 0.5

    def open_shutter(self):
        self.shutter = 32

    def close_shutter(self):
        self.shutter = 0

    def toggle_shutter(self):
        self.shutter = 32 - self.SHUTTER
        
    def get_gobo(self):
        val = self.gobo
        if self.gobo_shake and self.gobo not in [Gobo.OPEN, Gobo.SPIN]:
            val = val * 2 + 50
            
        return val
        
    def print(self, id=''):
        print(f'''-- {id} --
pan:    {self.pan}
tilt:   {self.tilt}
dimmer: {self.dimmer}
focus:  {self.focus}
color:  {self.color}
gobo:   {self.get_gobo()}
''')
        
LIGHTS = [Light(221), Light(201)]
CUR_LIGHT = [0]