from color import Color
import utime
import display
import urandom
import leds

gamma_table = [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                  0,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   2,   2,   2,   2,
                  2,   2,   2,   3,   3,   3,   3,   3,   4,   4,   4,   4,   5,   5,   5,   5,
                  6,   6,   6,   6,   7,   7,   7,   8,   8,   8,   9,   9,   9,  10,  10,  10,
                 11,  11,  11,  12,  12,  13,  13,  14,  14,  14,  15,  15,  16,  16,  17,  17,
                 18,  18,  19,  19,  20,  21,  21,  22,  22,  23,  23,  24,  25,  25,  26,  27,
                 27,  28,  29,  29,  30,  31,  31,  32,  33,  33,  34,  35,  36,  36,  37,  38,
                 39,  40,  40,  41,  42,  43,  44,  45,  45,  46,  47,  48,  49,  50,  51,  52,
                 53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,  65,  66,  67,  68,
                 69,  70,  71,  72,  74,  75,  76,  77,  78,  79,  81,  82,  83,  84,  86,  87,
                 88,  89,  91,  92,  93,  95,  96,  97,  99, 100, 101, 103, 104, 105, 107, 108,
                110, 111, 113, 114, 115, 117, 118, 120, 121, 123, 125, 126, 128, 129, 131, 132,
                134, 136, 137, 139, 140, 142, 144, 145, 147, 149, 151, 152, 154, 156, 158, 159,
                161, 163, 165, 166, 168, 170, 172, 174, 176, 178, 179, 181, 183, 185, 187, 189,
                191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 220, 222,
                224, 226, 228, 230, 232, 235, 237, 239, 241, 244, 246, 248, 250, 253, 255]

cols = [[0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0]]

i = 0

num_leds = float(len(cols))

disp = display.open()
leds.dim_top(4)


def get_rand_colour(j):
    spans_per_spectrum = 0.5
    cycles_per_loop = 1200.0

    space_offset = (j / (num_leds-1)) * spans_per_spectrum

    time_offset = (i % cycles_per_loop) / cycles_per_loop
    total_offset = (space_offset + time_offset) % 1.0
    # total_offset = time_offset
    # print("utime {} timeperloop {} utime%timeperloop {} uti%timeper/timeper {}".format(utime.time(), time_per_loop, utime.time()%time_per_loop, (utime.time()%time_per_loop) / time_per_loop))

    return [total_offset * 365,
            float(urandom.randrange(0,500)) / 1000.0 + 0.5,
            1.0]

def decay(x, factor):
    x[2] *= factor
    x[0] += 4
    return x

def draw_line(x, num_cols):
    pixels_per_led = 160 / num_cols

    disp.rect(int(x * pixels_per_led), 0, int((x+1)*pixels_per_led), 80, col=Color.from_hsv(*cols[x]))

def sparkle(sparkle_prob, decay_factor):
    global cols

    cols = list(decay(col, decay_factor) for col in cols)

    if urandom.randrange(0,101) < sparkle_prob * 100:
        index = urandom.randrange(0,len(cols))
    # for index in range(len(cols)):
        cols[index] = get_rand_colour(index)

    for j, col in enumerate(cols):
        leds.prep_hsv(j, col)

    leds.update()

while True:
    sparkle(0.5, 0.97)

    if i % 4 == 0:
        disp.clear()

        for j in range(11):
            draw_line(j, 11)

        disp.update()

    i += 1
