import ugfx, gc, wifi, badge, deepsleep, easydraw, system, time
from time import sleep, localtime
import urequests as requests

ugfx.init()

# Make sure WiFi is connected
try:
	wifi.init()
	ugfx.clear(ugfx.WHITE);
	ugfx.string(10,10,"Waiting for wifi...","Roboto_Regular12", 0)
	ugfx.flush()
 	while not wifi.sta_if.isconnected():
   		sleep(0.1)
except:
 	wifi.connect()
 	easydraw.messageCentered("\nConnecting...", True, "/media/wifi.png")
 	if not wifi.wait():
		easydraw.messageCentered("Could not connect!", True, "/media/alert.png")
		time.sleep(2)
		system.home()
	easydraw.messageCentered("\nConnected!", True, "/media/wifi.png")

icons = {
"clear-day": "\uf00d",
"clear-night": "\uf02e",
"rain": "\uf019",
"snow": "\uf01b",
"sleet": "\uf0b5",
"wind": "\uf050",
"fog": "\uf021",
"cloudy": "\uf041",
"partly-cloudy-day": "\uf002",
"partly-cloudy-night": "\uf086"
}

try:
	import consts
	consts.INFO_FIRMWARE_NAME
	#Workaround for bug in platform firmware...
	icons = {
	"clear-day": chr(1),
	"clear-night": chr(),
	"rain": chr(8),
	"snow": chr(15),
	"sleet": chr(15),
	"wind": chr(18),
	"fog": chr(17),
	"cloudy": chr(4),
	"partly-cloudy-day": chr(2),
	"partly-cloudy-night": chr(3)
	}
except:
  pass

def getDisplayName(time):
    #if time[3] == 0:
    #    n = "Night"
    #if time[3] == 6:
    #    n = "Morning"
    #if time[3] == 12:
    #    n = "Noon"
    #if time[3] == 18:
    #    n = "Evening"
    #return str(time[2]) + "." + str(time[1]) + ". " + n
    return str(time[2]) + "." + str(time[1]) + ". " + str(time[3]) + ":00"

# loading screen
ugfx.clear(ugfx.WHITE);
ugfx.string(10,10,"Getting weather from Dark Sky ...","Roboto_Regular12", 0)
ugfx.string(110,100,"https://darksky.net/poweredby/","Roboto_Regular12", 0)
ugfx.flush()

# fetching forecast data
url = "https://badge.sha2017.org/weather"
r = requests.get(url)
gc.collect()
data = r.json()
r.close()

# aligning according to time
hourly = data["hourly"]["data"]
starttime = localtime(hourly[0]["time"])
index = starttime[3]
if starttime[3] > 6:
    index = 12 - starttime[3]

if starttime[3] > 12:
    index = 18 - starttime[3]

if starttime[3] > 18:
    index = 24 - starttime[3]

print(index)
    
timepos = (10, 30)
iconpos = (10, 44)
temppos = (10, 105)

# displaying forecast
ugfx.clear();
ugfx.line(0, 26, 296, 26, 0)
ugfx.line(0, 44, 296, 44, 0)
ugfx.string(5, 5, "Weather Forecast for Zeewolde, NL        Powered by Dark Sky", "pixelade13", 0)

dp = hourly[index]
time = localtime(dp["time"])
ugfx.string(timepos[0], timepos[1], getDisplayName(time), "pixelade13", 0)
ugfx.string(iconpos[0], iconpos[1], icons[dp["icon"]], "weather42", 0)
ugfx.string(temppos[0], temppos[1], str(int(dp["temperature"]))+"°C", "DejaVuSans20", 0)

dp = hourly[index+6]
time = localtime(dp["time"])
ugfx.string(timepos[0] + 1*11 + 1*64, timepos[1], getDisplayName(time), "pixelade13", 0)
ugfx.string(iconpos[0] + 1*11 + 1*64, iconpos[1], icons[dp["icon"]], "weather42", 0)
ugfx.string(temppos[0] + 1*11 + 1*64, temppos[1], str(int(dp["temperature"]))+"°C", "DejaVuSans20", 0)

dp = hourly[index+12]
time = localtime(dp["time"])
ugfx.string(timepos[0] + 2*11 + 2*64, timepos[1], getDisplayName(time), "pixelade13", 0)
ugfx.string(iconpos[0] + 2*11 + 2*64, iconpos[1], icons[dp["icon"]], "weather42", 0)
ugfx.string(temppos[0] + 2*11 + 2*64, temppos[1], str(int(dp["temperature"]))+"°C", "DejaVuSans20", 0)

dp = hourly[index+18]
time = localtime(dp["time"])
ugfx.string(timepos[0] + 3*11 + 3*64, timepos[1], getDisplayName(time), "pixelade13", 0)
ugfx.string(iconpos[0] + 3*11 + 3*64, iconpos[1], icons[dp["icon"]], "weather42", 0)
ugfx.string(temppos[0] + 3*11 + 3*64, temppos[1], str(int(dp["temperature"]))+"°C", "DejaVuSans20", 0)

ugfx.flush(ugfx.LUT_FULL);


badge.eink_busy_wait()
deepsleep.start_sleeping(60000)

#ugfx.clear();
#ugfx.string(10,10,"SHA2017 ¯\_(ツ)_/¯","pixelade13", 0)
#ugfx.flush()