# -*- coding: utf-8 -*-
"""
Created on Thu Aug 17 22:25:52 2017

@author: arofarn
"""

###########
# IMPORTS #
###########

from cameteo import *
from flask import render_template
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import *

########
# MAIN #
########

bootstrap = Bootstrap(app)

nav = Nav()
nav.init_app(app)

navbar = Navbar('Camétéo', 
                View('Accueil', 'index'),
                View('Toutes les données', 'all_data'),
                Subgroup('Par données',
                        View("Température de l'air",  'by_data_type', dt='TA'),
                        View("Humidité relative",     'by_data_type', dt='HR'),
                        View("Pression atmosphérique",'by_data_type', dt='PA'),
                        ),
                Subgroup('Par capteur',                
                        View("Capteur BME280", 'by_sensor', sens = 'AdaBME280_1')
                        ),
                View('Configuration', 'config_page')
                )
                
nav.register_element('top', navbar)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/all_data')
def all_data():
    date_deb = datetime.utcnow()-timedelta(seconds=3600)
    res = Data.query.filter(Data.dbdate >= date_deb)
    return render_template('data_viz.html', dat=res.order_by(Data.dbdate.desc()).paginate(per_page=15))
     
@app.route('/type_id=<dt>')
def by_data_type(dt):
    date_deb = datetime.utcnow()-timedelta(seconds=3600)
    res = Data.query.filter(Data.type_id == dt).filter(Data.dbdate >= date_deb)
    return render_template('data_viz.html', dat=res.order_by(Data.dbdate.desc()).paginate(per_page=15))
    
@app.route('/sensor_id=<sens>')
def by_sensor(sens):
    date_deb = datetime.utcnow()-timedelta(seconds=3600)
    res = Data.query.filter(Data.sensor_id == sens).filter(Data.dbdate >= date_deb)
    return render_template('data_viz.html', dat=res.order_by(Data.dbdate.desc()).paginate(per_page=15))
    
@app.route('/configuration', methods=['GET', 'POST'])
def config_page():
    
    if request.method  == 'POST':
        pass
    else:
        pass
    
    return render_template('config_page.html')
    
if __name__=="__main__":
    app.run(host="0.0.0.0", debug=True)