import gc, wifi, display, buttons, eink, system
import urequests as requests

def clear_ghosting():
    display.drawFill(0xFFFFFF)
    display.flush(display.FLAG_LUT_NORMAL)
    display.drawFill(0x000000)
    display.flush(display.FLAG_LUT_NORMAL)
    eink.busy_wait()

def show_graph(output):
    clear_ghosting()
    display.drawFill(0xFFFFFF)
    gc.collect()
    display.drawPng(0,0,output.content)
    display.flush()

def show_screen(output):
    display.drawFill(0xFFFFFF)
    display.drawPng(0,0,basescreen3)
    display.drawPng(0,0,basescreen)
    radi = int(output[0]['cpm']) * 3 / 1000
    textA = str(output[0]['temperature'])+' '+str(output[0]['humidity'])+' '+str(output[0]['pm25'])
    textB = str(output[0]['voc'])+' '+str(output[0]['pressure'])
    textC = str(radi)
    display.drawText(0,78, textA, 0x000000, "PermanentMarker22")
    display.drawText(0,130-display.getTextWidth(textB, "PermanentMarker22"), textB, 0x000000, "PermanentMarker22")
    display.drawText(88,22, textC, 0x000000, "PermanentMarker22")
    display.flush(display.FLAG_LUT_GREYSCALE)

def refresh_data(pushed):
    if(pushed):
        get_data()

def go_home(pushed):
    if(pushed):
        system.launcher()

def get_data():
    r = requests.get(url+'/devices/64000025/all/last', headers=headers)
    gc.collect()
    global output
    output = r.json()
    r.close()
    show_screen(output)

def show_left(pushed):
    global counter_meas
    if pushed:
        counter_meas = counter_meas - 1
        if counter_meas < 0:
            counter_meas = 5
        output = requests.get(screen[counter_meas], headers=headers)
        gc.collect
        show_graph(output)

def show_right(pushed): 
    global counter_meas
    if pushed:
        counter_meas = counter_meas + 1
        if counter_meas > 5:
            counter_meas = 0
        output = requests.get(screen[counter_meas], headers=headers)
        gc.collect
        show_graph(output)



# Get Current Working Directory
try:
    cwd = '/'.join(__file__.split('/')[:-1])+'/'
except:
    cwd = '/'
	
with open(cwd+"basescreen.png", "rb") as fd:
    basescreen = fd.read()
    
with open(cwd+"basescreen3.png", "rb") as fd:
    basescreen3 = fd.read()
    
clear_ghosting()

# Make sure WiFi is connected
while not wifi.status():
  wifi.connect()
  display.drawFill(0xFFFFFF)
  display.drawPng(0,0,basescreen)
  display.drawText(10,60, "Waiting for wifi...", 0x000000, "Roboto_Regular12")
  display.flush()
  wifi.wait()

# loading screen
clear_ghosting()
display.drawFill(0xFFFFFF)
display.drawPng(0,0,basescreen)
display.drawText(10,60, "Getting uRadMonitor data...", 0x000000, "Roboto_Regular12")
display.drawText(40,100, "https://uradmonitor.com/", 0x000000, "Roboto_Regular12")
display.flush()

#Json request info
headers = {'X-User-id': 'sha2017', 'X-User-hash': 'badge'}
url = 'https://data.uradmonitor.com/api/v1'

#unit = {0: '64000024', 1: '64000025', 2: '82000065'}
unit_val = '64000025'
duration = '36000'
counter_meas = 5
screen_1 = url+'/chart/'+unit_val+'/temperature/'+duration
screen_2 = url+'/chart/'+unit_val+'/humidity/'+duration
screen_3 = url+'/chart/'+unit_val+'/pm25/'+duration
screen_4 = url+'/chart/'+unit_val+'/voc/'+duration
screen_5 = url+'/chart/'+unit_val+'/pressure/'+duration
screen_6 = url+'/chart/'+unit_val+'/cpm/'+duration
#screen_7 = url+'/chart/'+unit_val+'/formaldehyde/'+duration
#screen_8 = url+'/chart/'+unit_val+'/carbondioxide/'+duration

screen = {0: screen_1, 1: screen_2, 2: screen_3, 3: screen_4, 
          4: screen_5, 5: screen_6} #, 6: screen_7, 7: screen_8}

get_data()

buttons.attach(buttons.BTN_A, refresh_data)
buttons.attach(buttons.BTN_B, go_home)
buttons.attach(buttons.BTN_START, go_home)
buttons.attach(buttons.BTN_LEFT, show_left)
buttons.attach(buttons.BTN_RIGHT, show_right)