import color
import os
import display
import utime
import buttons

def i2s(input):
    ret_i = int(input)
    if ret_i < 10:
        return "0" + str(ret_i)
    else:
        return str(ret_i)

def render_display():
    with display.open() as d:
        d.clear()
        act_x = 15
        act_y = 4
        act_font = display.FONT20
        act_font_width = 14
        act_sep = "-"
        for i in range(6): # only first 6 are important for us
            if i == act_section:
                col_fg = color.CYAN
                col_bg = color.BLUE
            else:
                col_fg = color.BLUE
                col_bg = None
            if i == 3:
                act_x = 17
                act_y = 34
                act_font = display.FONT24
                act_font_width = 16
                act_sep = ":"
            act_val = i2s(set_time[i])
            d.print(act_val,fg=col_fg,bg=col_bg,font=act_font,posx=act_x,posy=act_y)
            act_x += act_font_width * len(act_val)
            if i not in [2,5]: 
                d.print(act_sep,fg=color.WHITE,font=act_font,posx=act_x,posy=act_y)
                act_x += act_font_width
        if act_section == 6:
            d.print("[set]",fg=color.BLACK,bg=color.GREEN,font=display.FONT20,posx=48,posy=64)
        else:
            d.print("[set]",fg=color.GREEN,font=display.FONT20,posx=48,posy=64)
        d.update()

def inc_section(number):
    temp = set_time[number]
    temp += 1
    if number == 1:
        if temp > 12:
            temp = 1
    elif number == 2:
        if temp > 31:
            temp = 1
    elif number == 3:
        if temp > 23:
            temp = 0
    elif number in [4,5]:
        if temp > 59:
            temp = 0
    set_time[number] = temp

def dec_section(number):
    temp = set_time[number]
    temp -= 1
    if number == 0:
        if temp < 2000:
            temp = 2019
    elif temp < 1:
        if number == 1:
            temp = 12
        elif number == 2:
            temp = 31
        elif temp < 0:
            if number == 3:
                temp = 23
            elif number in [4,5]:
                temp = 59
    set_time[number] = temp

def set_newtime():
    utime.set_time(utime.mktime(set_time))
    with display.open() as d:
        d.clear()
        d.print("Success!",fg=color.GREEN,font=display.FONT24,posx=14,posy=28)
        d.update()
    utime.sleep(1.0)
    os.exit(0)

set_time = list(utime.localtime())
act_section = 0

render_display()

while True:
    pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT)
    if pressed != 0:
        if pressed & buttons.BOTTOM_LEFT != 0:
            if act_section == 6:
                set_newtime()
            else:
                dec_section(act_section)
        elif pressed & buttons.BOTTOM_RIGHT != 0:
            if act_section == 6:
                set_newtime()
            else:
                inc_section(act_section)
        elif pressed & buttons.TOP_RIGHT != 0:
            if act_section < 6:
                act_section += 1
            else:
                act_section = 0
        render_display()
    
    utime.sleep(0.2)