## Set up your timezone from the terminal by entering:
## machine.nvs_setstr('system', 'timezone', 'YOURTIMEZONE')
## Get your YOURTIMEZONE from
## https://remotemonitoringsystems.ca/time-zone-abbreviations.php

import time, rgb, wifi, buttons, defines, system, random, machine
from default_icons import animation_connecting_wifi, icon_no_wifi

direction = 0
tries = 1
exit = 0
brightness = rgb.getbrightness()
rgb.background((0,0,0))
rgb.clear()
maxcolor = 180
color = (0, 180, 0)

rtc=machine.RTC()
timezone = machine.nvs_getstr('system', 'timezone')
if timezone is None:
  #timezone = 'GMT+0BST-1,M3.5.0/01:00:00,M10.5.0/02:00:00'
  timezone = 'CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00' # Amsterdam,Netherlands
machine.RTC().timezone(timezone)

while not wifi.status():
    rgb.setfont(rgb.FONT_6x3)
    trietext = "Trie {}".format(tries)
    print(trietext)
    rgb.text(trietext, (50, 50, 50), (0, 0))
    time.sleep(1)
    data, size, frames = animation_connecting_wifi
    rgb.clear()
    rgb.framerate(3)
    rgb.gif(data, (12, 0), size, frames)
    wifi.connect()
    if wifi.wait():
        rgb.clear()
        rgb.framerate(20)
    else:
        msg = "No wifi"
        print(msg)
        #rgb.scrolltext(msg, (50, 50, 50), (0, 0))
        #time.sleep(5)
        rgb.clear()
        rgb.framerate(20)
        data, frames = icon_no_wifi
        rgb.image(data, (12, 0), (8,8))
        time.sleep(3)
        rgb.clear()
    time.sleep(3)
    tries = tries + 1

while rtc.now()[0]==1970:
    rtc.ntp_sync('pool.ntp.org')
#if not ntp.set_NTP_time():
#    print("Error setting time")
#    system.reboot()

#good random int source to use
#random.seed()

rgb.setfont(rgb.FONT_7x5)
wifi.disconnect()

UP, DOWN, LEFT, RIGHT = defines.BTN_UP, defines.BTN_DOWN, defines.BTN_LEFT, defines.BTN_RIGHT
A, B = defines.BTN_A, defines.BTN_B

def input_B(pressed):
    global direction
    direction = B

def callback_btn_UP(button_is_down):
    if button_is_down:
        global maxcolor, color
        col1 = random.randrange(0, maxcolor)
        col2 = random.randrange(0, maxcolor)
        col3 = random.randrange(0, maxcolor)
        msg = "Color RGB {}, {}, {}".format(col1, col2, col3)
        print(msg)
        color = (col1, col2, col3)

def callback_btn_LEFT(button_is_down):
    if button_is_down:
        global brightness
        if brightness > 3:
            brightness = brightness - 1
            rgb.brightness(brightness)
            msg = "Brightness {}".format(brightness)
            print(msg)

def callback_btn_RIGHT(button_is_down):
    if button_is_down:
        global brightness
        if brightness < 32:
            brightness = brightness + 1
            rgb.brightness(brightness)
            msg = "Brightness {}".format(brightness)
            print(msg)

def callback_btn_B(button_is_down):
    if button_is_down:
        print("callback_btn_B")
        global exit
        exit = 1

buttons.register(defines.BTN_UP, callback_btn_UP)
buttons.register(defines.BTN_LEFT, callback_btn_LEFT)
buttons.register(defines.BTN_RIGHT, callback_btn_RIGHT)
buttons.register(defines.BTN_B, callback_btn_B)

tmold = 99
rgb.clear()
rgb.framerate(1)
showcolon = True

while not exit == 1:
    showcolon = not showcolon
    th = rtc.now()[3]
    tm = rtc.now()[4]
    #th = time.strftime("%H")
    #tm = time.strftime("%M")
    
    if showcolon: 
        totaltime = "{:0>2}".format(th) + ":" + "{:0>2}".format(tm)
    else:
        totaltime = "{:0>2}".format(th) + " " + "{:0>2}".format(tm)
    #print(totaltime)
	
    if totaltime != tmold:
        rgb.clear()
        rgb.text(totaltime, color, (1, 0))
        tmold = totaltime
    time.sleep(1)
    
system.reboot()