from machine import Pin, SPI
from time import sleep
import ssd1322
globalKeyOrg = [128, 36]
arrMacroSelect = [200,47]
arrBrightSelect = [25,47]
arrSelectedMacroSet = [50,10]
intWordLen = 7
intCharW = 7.5
intCharH = 7.5
intBrightness = 15
spi=SPI(1,8_000_000)
cs=Pin(12,Pin.OUT)
dc=Pin(15,Pin.OUT)
res=Pin(14,Pin.OUT)
disp=ssd1322.SSD1322_SPI(256,64,spi,dc,cs,res)
dictKeyCord = {'A1': [globalKeyOrg[0] - 95, globalKeyOrg[1] - 18],
               'A2': [globalKeyOrg[0] - 32, globalKeyOrg[1] - 18],
               'A3': [globalKeyOrg[0] + 32, globalKeyOrg[1] - 18],
               'A4': [globalKeyOrg[0] + 95, globalKeyOrg[1] - 18],
               'B1': [globalKeyOrg[0] - 95, globalKeyOrg[1] + 10],
               'B2': [globalKeyOrg[0] - 32, globalKeyOrg[1] + 10],
               'B3': [globalKeyOrg[0] + 32, globalKeyOrg[1] + 10],
               'B4': [globalKeyOrg[0] + 95, globalKeyOrg[1] + 10]}

def brightness(strDirection):
    global intBrightness
    if strDirection == 'UP':
        intBrightness = intBrightness + 1
    else:
        intBrightness = intBrightness - 1
    if intBrightness <= 3: intBrightness = 3
    if intBrightness >= 16: intBrightness = 15

def loading(strStage, strVersion):
    disp.fill(0)
    disp.text('Stage: ' + strStage,0,0,intBrightness)
    disp.text('Version: ' + strVersion,150,0,intBrightness)
    disp.show()

def displayMacros(objMacro):
    disp.fill(0)
    #disp.show()
    #Create Grid
    arrKeys = objMacro.keys
    disp.line(0,0,0,64,intBrightness) #|Left line
    disp.line(0,8,256,8,intBrightness) #-Top Line
    disp.line(255,0,255,64,intBrightness) #|Right Line
    disp.line(0,63,255,63,intBrightness) #-Bottom Line
    disp.line(0,35,255,35,intBrightness) #- Center Line
    disp.line(64,8,64,64,intBrightness) #|Left Center Line
    disp.line(128,8,128,64,intBrightness) #| Center Line
    disp.line(192,8,192,64,intBrightness) #|Right Center Line
    disp.text('MACRO: ' + objMacro.caption,2,0,intBrightness)
    
    for obj in arrKeys:
        strKey = obj.settingclass
        strCaption = obj.caption
        arrWords = strCaption.split(" ")
        while len(arrWords) >= 4:
            arrWords.pop(len(arrWords) - 1)
        for index in range(0, len(arrWords)):
            if len(arrWords[index]) > intWordLen:
                arrWords[index] = arrWords[index][:intWordLen - 2] + '..'
        if len(arrWords) == 1:
            #LINE 1
            intOffSet = round(dictKeyCord[strKey][0] - ((len(arrWords[0]) / 2) * (intCharW + .5)))
            disp.text(arrWords[0], intOffSet, dictKeyCord[strKey][1],intBrightness)
        elif len(arrWords) == 2:
            #LINE 1
            intOffSet = round(dictKeyCord[strKey][0] - ((len(arrWords[0]) / 2) * (intCharW + .5)))
            disp.text(arrWords[0], intOffSet, round(dictKeyCord[strKey][1] - intCharH/2),intBrightness)
            #LINE 2
            intOffSet = round(dictKeyCord[strKey][0] - ((len(arrWords[1]) / 2) * (intCharW + .5)))
            disp.text(arrWords[1], intOffSet, round(dictKeyCord[strKey][1] + intCharH /2),intBrightness)
        elif len(arrWords) == 3:
            #LINE 1
            intOffSet = round(dictKeyCord[strKey][0] - ((len(arrWords[0]) / 2) * (intCharW + .5)))
            disp.text(arrWords[0], intOffSet, round(dictKeyCord[strKey][1] - intCharH),intBrightness)
            #LINE 2
            intOffSet = round(dictKeyCord[strKey][0] - ((len(arrWords[1]) / 2) * (intCharW + .5)))
            disp.text(arrWords[1], intOffSet, dictKeyCord[strKey][1],intBrightness)
            #LINE 3
            intOffSet = round(dictKeyCord[strKey][0] - ((len(arrWords[2]) / 2) * (intCharW + .5)))
            disp.text(arrWords[2], intOffSet, round(dictKeyCord[strKey][1] + intCharH),intBrightness)
    disp.show()
    
def intMenu(strSelectedMacro):
    disp.fill(0)
    disp.show()
    # Select Macro Button
    disp.text('SELECT',arrMacroSelect[0], arrMacroSelect[1],intBrightness)
    disp.text('MACRO',round(arrMacroSelect[0] + intCharW / 2), round(arrMacroSelect[1] + intCharH + 1),intBrightness)
    # Set Brightness Buttons
    disp.text('BRIGHTNESS',arrBrightSelect[0], arrBrightSelect[1],intBrightness)
    disp.text('SCRN  KEYS',round(arrBrightSelect[0]), round(arrBrightSelect[1] + intCharH + 1),intBrightness)
    
    # Selected Macro
    disp.text('SELECTED MACRO:',arrSelectedMacroSet[0], arrSelectedMacroSet[1],intBrightness)
    disp.text(strSelectedMacro,round(arrSelectedMacroSet[0]), round(arrSelectedMacroSet[1] + intCharH + 5),intBrightness)
    disp.show()
    
def updateMenu(strNewMSet, strRemoveMSet):
    disp.text(strRemoveMSet,round(arrSelectedMacroSet[0]), round(arrSelectedMacroSet[1] + intCharH + 5),0)
    disp.text(strNewMSet,round(arrSelectedMacroSet[0]), round(arrSelectedMacroSet[1] + intCharH + 5),intBrightness)
    disp.show()
