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

# Icons
fs_sol_icon = 'https://bastart.spoton.cz/static/img/iss_solar_icon.png'
fs_cap_icon = 'https://bastart.spoton.cz/static/img/iss_capacitor_icon.png'


class StatusInfo(object):
    def __init__(self):
        self.measures_obj = dynamic.DynamicStatus()
        
    @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 = '',
                               status = 'active', 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('status_admin', config._templates)
        current_values = self.measures_obj.FreshValues(type='dict')
        uptime_since = datetime.datetime.strftime(
            datetime.datetime.fromtimestamp(int(psutil.boot_time())),
            "%Y-%m-%d %H:%M:%S"
        )
        admin_html = admin_preformat.format(
                timestamp = current_values['time'],
                sol_icon = fs_sol_icon,
                cap_icon = fs_cap_icon,
                cpu_icon = '',
                mem_icon = '',
                network_icon = '',
                _sol_value = current_values['iss_solar'],
                _cap_value = current_values['iss_capacitor'],
                _cpu = current_values['RasPI_cpu'],
                _mem = current_values['RasPI_mem'],
                _disk = current_values['RasPI_disk'],
                _net_in = current_values['net']['b_in'],
                _net_out = current_values['net']['b_out'],
                _uptime = uptime_since
                )
        return admin_html
