#!/usr/bin/env python3
import sys
import os
from time import strftime
import time
from math import ceil

#Nx = int(sys.argv[1])
#Ny = int(sys.argv[2])
#param = sys.argv[3]
Nx = 80
Ny = 40


buffer = bytearray(b"\x00"*(3*Nx*Ny))

def SetPixel(x, y, r, g, b):
    idx = 3*(x+Nx*y)
    buffer[idx+0] = r
    buffer[idx+1] = g
    buffer[idx+2] = b

def two(r,g,b):
    for y in range (1,15):
        for x in range (9,23):
            SetPixel(x,y,r,g,b)

def three(r,g,b):
    for y in range (17,39):
        for x in range (9,31):
            SetPixel(x,y,r,g,b)

def one(r,g,b):
    for y in range (1,7):
        for x in range (25,31):
            SetPixel(x,y,r,g,b)

def one_one(r,g,b):
    for y in range (9,15):
        for x in range (25,31):
            SetPixel(x,y,r,g,b)

def five(r,g,b):
    for y in range (1,39):
        for x in range (33,71):
            SetPixel(x,y,r,g,b)

def and_gate(hourcol, mincol):
    if hourcol and mincol:
        return 'y'
    if hourcol:
        return 'r'
    if mincol:
        return 'g'
    if not hourcol and not mincol:
        return 'b'

def get_colsquares(hour, minute):
    coldict = {
        0: [0, 0, 0, 0, 0],
        1: [0, 0, 0, 0, 1],
        2: [0, 0, 0, 1, 1],
        3: [0, 0, 1, 1, 0],
        4: [0, 0, 1, 1, 1],
        5: [0, 1, 1, 0, 0],
        6: [0, 1, 1, 1, 0],
        7: [0, 1, 1, 1, 1],
        8: [1, 1, 0, 0, 0],
        9: [1, 1, 0, 1, 0],
        10: [1, 1, 1, 0, 0],
        11: [1, 1, 1, 1, 0],
        12: [1, 1, 1, 1, 1]
    }

    i = 0
    result = []
    while i < 5:
        result.append( and_gate(coldict[hour][i], coldict[minute][i]) )
        i += 1
    return result

def gettime():
    hour = int(strftime('%I'))
    minute12 = ceil(int(strftime('%M'))/5.0)
    minute = int(strftime('%M'))
    
    return hour, minute12, minute

while True:
    rgbw = {
        'r' : (255, 0, 0),
        'g' : (0, 255, 0),
        'b' : (0, 0, 255),
        'y' : (255, 255, 0)
        }

    hour, minute12, minute = gettime()
    colsq = get_colsquares(hour, minute12)


    five(rgbw[colsq[0]][0],rgbw[colsq[0]][1],rgbw[colsq[0]][2])
    three(rgbw[colsq[1]][0],rgbw[colsq[1]][1],rgbw[colsq[1]][2])
    two(rgbw[colsq[2]][0],rgbw[colsq[2]][1],rgbw[colsq[2]][2])
    one_one(rgbw[colsq[3]][0],rgbw[colsq[3]][1],rgbw[colsq[3]][2])
    one(rgbw[colsq[4]][0],rgbw[colsq[4]][1],rgbw[colsq[4]][2])

    os.write(1, buffer)
    time.sleep(1)
    #grid()
