#!/usr/bin/env python
#version    :       2023.04.03
#language   :       en
from maix import display
from maix import image
from maix import camera
import bs4
import ssl
import json
ssl._create_default_https_context = ssl._create_unverified_context
import http.client
camera.camera.config(size=(320,240))

ScreenOrientation = False
image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")
image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")

def get_content(city):
    weather_list = []
    try:        
        conn = http.client.HTTPSConnection("weather.visualcrossing.com")
        conn.request("GET", "/VisualCrossingWebServices/rest/services/timeline/"+city+"?unitGroup=us&include=days&key=XGMGVZSNH3RFJWNQYL4Z5THYS&contentType=json")
        res = conn.getresponse()
        data = res.read().decode("utf-8")
        # print(json.loads(data)["days"],'1111111111111111111')
        for i in json.loads(data)["days"]:
            # print(i)
            weather_list.extend([{"D":i["datetime"],"W":i["conditions"],
            "T":str(round((i["tempmax"]-32)/1.8))+"/"+str(round((i["tempmin"]-32)/1.8))+"℃"}])
    # weather_list = data.decode("utf-8")["days"]
    except:
        print('no result')
    # with open("/root/wResult.txt","w") as f:
    #     f.write(data.decode("utf-8"))
    # print(weather_list)
    return weather_list

# https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/weatherdata/forecast?locations=Herndon,VA,20170&aggregateHours=24&unitGroup=us&shortColumnNames=false&contentType=csv&key=YOURAPIKEY

canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
canvas.draw_string(4 , 100 , "Checking Wi-Fi connection", scale = 1, color = (255,255,255), thickness = 2)
display.show(canvas)
weather_all1 = get_content('shenzhen')
weather_all2 = get_content('HongKong')
weather_all3 = get_content('Macau')
weather_all4 = get_content('Guangzhou')
while True:
    canvas.clear()
    try:
        canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
        #canvas.draw_string(2,20, ("Day:"+str(weather_all1[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(2,20, ("W/W:"+str(weather_all1[0]["W"])+"  ,T:"+str(weather_all1[0]["T"]))[:-1]+"'C", scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(0,0, "Shenzhen", scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(0,60, "Hong Kong", scale = 1, color = (255,255,255) , thickness = 1)
        #canvas.draw_string(2,80, ("Day:"+str(weather_all2[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(2,80, ("W/W:"+str(weather_all2[0]["W"])+"  ,T:"+str(weather_all2[0]["T"]))[:-1]+"'C", scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(0,120, "Macau", scale = 1, color = (255,255,255) , thickness = 1)
        #canvas.draw_string(2,140, ("Day:"+str(weather_all3[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(2,140, ("W/W:"+str(weather_all3[0]["W"])+"  ,T:"+str(weather_all3[0]["T"]))[:-1]+"'C", scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(0,180, "Guangzhou", scale = 1, color = (255,255,255) , thickness = 1)
        #canvas.draw_string(2,200, ("Day:"+str(weather_all4[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
        canvas.draw_string(2,200, ("W/W:"+str(weather_all4[0]["W"])+"  ,T:"+str(weather_all4[0]["T"]))[:-1]+"'C",scale = 1, color = (255,255,255) , thickness = 1)
    except:
        canvas.draw_string(20,80, "Get weather information failed!", scale = 1, color = (255,255,255) , thickness = 1)
    if ScreenOrientation:
        canvasVER = canvas.crop(0,0,240,320)
        canvasVER = canvasVER.rotate(-90, adjust=1)
        display.show(canvasVER)
    else:
        canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
        display.show(canvas)
