import system, tasks.powermanagement as pm, buttons, defines
import utime
import urequests
import rgb
import wifi
import time
import math
from default_icons import animation_connecting_wifi, icon_no_wifi

icon = [0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0xf8e02cff, 0x000000ff, 0x000000ff, 0x000000ff, 0x51feb2ff, 0x51feb2ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x51feb2ff, 0x51feb2ff, 0x000000ff, 0x000000ff, 0xf8e02cff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x51feb2ff, 0x51feb2ff, 0x51feb2ff, 0x000000ff, 0x51feb2ff, 0x51feb2ff, 0x51feb2ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x51feb2ff, 0x51feb2ff, 0x000000ff, 0x51feb2ff, 0x51feb2ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x51feb2ff, 0x51feb2ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x51feb2ff, 0x000000ff, 0x000000ff, 0xf8e02cff, 0x000000ff, 0x51feb2ff, 0x51feb2ff, 0x000000ff, 0x000000ff, 0x51feb2ff, 0x51feb2ff, 0x51feb2ff, 0x000000ff, 0xf8e02cff, 0x000000ff, 0x000000ff, 0x000000ff, 0x51feb2ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x51feb2ff, 0x51feb2ff, 0x51feb2ff, 0x000000ff, 0x51feb2ff, 0x51feb2ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x51feb2ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x51feb2ff, 0x51feb2ff, 0x51feb2ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x51feb2ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x51feb2ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0xf6e039ff, 0x000000ff, 0x000000ff, 0xf6e039ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x55fab1ff, 0x55fab1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x55fab1ff, 0x55fab1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0xf6e039ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x55fab1ff, 0x55fab1ff, 0x55fab1ff, 0x000000ff, 0x55fab1ff, 0x55fab1ff, 0x55fab1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x55fab1ff, 0x55fab1ff, 0x55fab1ff, 0x55fab1ff, 0x55fab1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x55fab1ff, 0x55fab1ff, 0x000000ff, 0xf6e039ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x55fab1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0xf6e039ff, 0x55fab1ff, 0x55fab1ff, 0x000000ff, 0x000000ff, 0x55fab1ff, 0x55fab1ff, 0x55fab1ff, 0x000000ff, 0x000000ff, 0xf6e039ff, 0x000000ff, 0x000000ff, 0x55fab1ff, 0x000000ff, 0x000000ff, 0xf6e039ff, 0x000000ff, 0x55fab1ff, 0x55fab1ff, 0x55fab1ff, 0x55fab1ff, 0x55fab1ff, 0x55fab1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x55fab1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x55fab1ff, 0x55fab1ff, 0x55fab1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0xf5e044ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x59f8b1ff, 0x59f8b1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x59f8b1ff, 0x59f8b1ff, 0x000000ff, 0x000000ff, 0xf5e044ff, 0x000000ff, 0x000000ff, 0x000000ff, 0xf5e044ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x59f8b1ff, 0x59f8b1ff, 0x000000ff, 0x59f8b1ff, 0x59f8b1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0xf5e044ff, 0x000000ff, 0x000000ff, 0x59f8b1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x59f8b1ff, 0x59f8b1ff, 0x000000ff, 0xf5e044ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x59f8b1ff, 0x000000ff, 0x000000ff, 0xf5e044ff, 0x000000ff, 0x59f8b1ff, 0x59f8b1ff, 0x000000ff, 0x000000ff, 0x59f8b1ff, 0x59f8b1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x59f8b1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0xf5e044ff, 0x000000ff, 0x59f8b1ff, 0x59f8b1ff, 0x59f8b1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x59f8b1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x59f8b1ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff]

def input_left(pressed):
  pm.feed()
  if pressed:
      rgb.setbrightness(rgb.getbrightness() - 2)

def input_right(pressed):
  pm.feed()
  if pressed:
      rgb.setbrightness(rgb.getbrightness() + 2)

buttons.register(defines.BTN_LEFT, input_left)
buttons.register(defines.BTN_RIGHT, input_right)

if not wifi.status():
    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:
        print('No wifi')
        rgb.clear()
        rgb.framerate(20)
        data, frames = icon_no_wifi
        rgb.image(data, (12, 0), (8,8))
        time.sleep(3)
        rgb.clear()

if not wifi.status():
    print("Error connecting to wifi")
    system.reboot()

request = urequests.get('http://worldtimeapi.org/api/ip')
currentDateInSeconds = request.json()['unixtime']
startDate = utime.mktime([2020, 3, 16, 1, 0, 0, 2, 76])
dayInSeconds = 86400
secondsPassed = currentDateInSeconds - startDate
daysPassed = math.ceil(secondsPassed / dayInSeconds)
daysInString = str(daysPassed)

rgb.clear()
rgb.framerate(2)
rgb.gif(icon, (0, 0), (17, 8), 3)
rgb.text(daysInString, (255,255,255), (17,1))