from pyweb import pydom
from pyscript import document
import locale
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')


from client import MeteoFranceClient
from const import METEOFRANCE_API_URL

def q(selector, root=document):
    return root.querySelector(selector)

def add_previ(descript,tps_previ,jr_nt,previ_icon):

    # create previ
    previ_id = f"previ-{len(previs)}"
    previ = {
        "id": previ_id,
        "previ_heure" : tps_previ,
        "content": descript,
        "j_n" : jr_nt,
        "pr_icon" : previ_icon,
    }

    previs.append(previ)

    # add the task element to the page as new node in the list by cloning from a
    # template
    previ_html = previ_template.clone()
    previ_html.id = previ_id

    #previ_html_check = previ_html.find("input")[0]
    
    previ_html_content = previ_html.find("p")[0]
    previ_html_content._js.textContent = previ["content"]

    previ_html_content = previ_html.find("p1")[0]
    previ_html_content._js.textContent = previ["previ_heure"] 
    txt1 = previ["pr_icon"]
    previ_html_content = previ_html.find("img")[0]
    
    previ_html_content._js.srcset=txt1

    
    previ_list.append(previ_html) 

def j_n(dt, d_sun_rise, d_sun_set):
    if dt < d_sun_rise and dt > d_sun_set : return 0
    else : return 1


previ_template = pydom.Element(q("#previ-template").content.querySelector(".previ"))

previ_list = pydom["#list-previ-container"][0]

new_previ_content = pydom["#new-previ-content"][0]

previs = []



client = MeteoFranceClient()

places = client.search_places('Saint-Martin-de-Caralp')
obs = client.get_observation_for_place(places[0])
fcast = client.get_forecast_for_place(places[0])
rain = client.get_rain(places[0].latitude, places[0].longitude, language='fr')
pict_of_day = client.get_picture_of_the_day()

pydom["div#obsv_date"].html=obs.time_as_datetime.strftime('%a %d %B %Y, %H:%M ')
pydom["div#obsv_txt"].html = obs.weather_description
pydom["div#obsv_icon_txt"].html = obs.weather_icon

txt_obs_icon = obs.weather_icon
txt_fich_obs_icon = f"./assets/icones/mt_f/{txt_obs_icon}.png"
print(txt_fich_obs_icon)
xbox = pydom.Element(q("#img3"))#.content.querySelector(".previ"))
xbox._js.srcset=txt_fich_obs_icon

pydom["div#obsv_teta"].html = obs.temperature
pydom["div#obsv_wd_speed"].html = obs.wind_speed
pydom["div#obsv_wd_dir"].html = obs.wind_direction
pydom["div#obsv_wd_icon"].html = obs.wind_icon

# prévision journalière et variables jour/nuit
to_d_fcast = fcast.today_forecast
h_sun_rise = rain.timestamp_to_locale_time(to_d_fcast.get('sun').get('rise')).strftime('%a %d %B %Y, %H:%M ')
h_sun_set = rain.timestamp_to_locale_time(to_d_fcast.get('sun').get('set')).strftime('%a %d %B %Y, %H:%M ')


dt = rain.timestamp_to_locale_time(to_d_fcast.get('dt'))

#print(" Prévisions  établies à :  ", )
pydom["div#previ_date"].html = dt.strftime('%a %d %B %Y, %H:%M ')

#print("     Températures. mini : ", (, " maxi : ",)
pydom["div#previ_teta_min"].html=to_d_fcast.get('T').get('min')
pydom["div#previ_teta_max"].html=to_d_fcast.get('T').get('max')

#print("     Humidité. mini : ", (, " maxi : ",
pydom["div#previ_hr_min"].html=to_d_fcast.get('humidity').get('min')
pydom["div#previ_hr_max"].html=to_d_fcast.get('humidity').get('max')

#print("     Précipitations : ", () print("     UV : ", )
pydom["div#previ_precip_24"].html=to_d_fcast.get('precipitation').get('24h')
pydom["div#previ_uv"].html=to_d_fcast.get('uv')

#print("     Icone  : ", , " descriptif : ",to_d_fcast.get('weather12H').get('desc'))
txt_icon=to_d_fcast.get('weather12H').get('icon')        

#print(" Lever de soleil : ", , "coucher : "  
pydom["div#previ_hr_rise"].html=rain.timestamp_to_locale_time(to_d_fcast.get('sun').get('rise')).strftime(' %H:%M ')
pydom["div#previ_hr_set"].html=rain.timestamp_to_locale_time(to_d_fcast.get('sun').get('set')).strftime(' %H:%M ')

#pict of day url de l'image et description
pydom["div#pict_url"].html=pict_of_day.image_url
pydom["div#pict_desc"].html=pict_of_day.description

# pluies dans l'heure à venir
next_rain = rain.next_rain_date_locale()
xstr_icon = ""
for tbl in rain.forecast:
        date = rain.timestamp_to_locale_time(tbl.get('dt'))
        xdate = date.strftime('%a %d %B %Y, %H:%M ')
        xj_n = j_n(xdate,h_sun_rise,h_sun_set)
        txt_fich_obs_icon = f"./assets/icones/mt_f/{txt_obs_icon}.png"
        add_previ(tbl.get('desc'),xdate,xj_n,txt_fich_obs_icon)

        #print(" prévision à : ", (rain.timestamp_to_locale_time(tbl.get('dt'))).strftime('%a %d %B %Y, %H:%M '), " ", tbl.get('desc'))
        #pydom["th#heure_previ"].html = rain.timestamp_to_locale_time(tbl.get('dt'))
        #pydom["td#previ"].html = tbl.get('desc')
