#!/usr/bin/python3
from modules import dynamic
import cherrypy
import time
import config

# Icons
base_url = 'https://bastart.spoton.cz/static'
fs_wind_icon = base_url + '/img/wind_icon.png'
fs_pressure_icon = base_url + '/img/pressure_icon.png'
fs_in_temperature_icon = base_url + '/img/inside_temp_icon.png'
fs_out_temperature_icon = base_url + '/img/outside_temp_icon.png'
fs_windgust_icon = base_url + '/img/wind_gust_icon.png'
fs_winddirection_icon = base_url + '/img/wind_direction_icon.png'
fs_rain_rate_icon = base_url + '/img/rain_rate_icon.png'


class WeatherInfo(object):
    def __init__(self):
        self.measures_obj = dynamic.DynamicWeather()
        
    @cherrypy.expose
    def index(self):
        header = config.read_html('header', config._templates)
        menu_raw = config.read_html('top_menu', config._templates)
        menu = menu_raw.format(energy = '', weather = 'active',
                               status = '', temphumi = '')
        body = self.body()
        footer = config.read_html('footer', config._templates)
        result = header\
               + menu\
               + body\
               + footer
        return result

    def body(self):
        admin_preformat = config.read_html('weather_admin', config._templates)
        current_weather = self.measures_obj.FreshValues(type='dict')
        admin_html = admin_preformat.format(
                _timestamp = current_weather['time'],
                _w_speed_icon = fs_wind_icon,
                _w_speed_km = current_weather['wind']['speed'],
                _w_speed_ms = round(current_weather['wind']['speed_ms'], 1),
                _w_gust_icon = fs_windgust_icon,
                _w_gust_km = current_weather['wind']['gust'],
                _w_gust_ms = \
                    round(current_weather['wind']['gust_ms'], 1),
                _w_dir_icon = fs_winddirection_icon,
                _w_dir_name = current_weather['wind']['dir_name'],
                _w_dir_deg = current_weather['wind']['dir'],
                _out_temp_icon = fs_out_temperature_icon,
                _out_temp = current_weather['temperature']['out'],
                _in_temp_icon = fs_in_temperature_icon,
                _in_temp = current_weather['temperature']['in'],
                _pressure_icon = fs_pressure_icon,
                _pressure = current_weather['pressure'],
                _raw_pressure = current_weather['pressure'],
                _rrate_icon = fs_rain_rate_icon,
                _rrate = current_weather['rainrate']
                )
        return admin_html
