# -*- encoding: utf-8 -*-
"""
License: MIT
Copyright (c) 2019 - present AppSeed.us
"""

from app.home import blueprint
from flask import render_template, redirect, url_for
from flask_login import login_required, current_user
from app import login_manager
from jinja2 import TemplateNotFound
import json
import requests
import random
import datetime




aPythonVar = "doraemon"

@blueprint.route('/index')
# @login_required
def index():
    
    #if not current_user.is_authenticated:
    #    return redirect(url_for('base_blueprint.login'))

    fromDate = "2020-01-01"
    page = requests.get("https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=%s&minlatitude=17&maxlatitude=19&minlongitude=-68&maxlongitude=-64" % (fromDate))

    
    x = []
    y = []
    print(page.content)
    parsed_json = (json.loads(page.content.decode("utf-8")))
    
    for f in parsed_json["features"]:
        p = f['properties']
        if p['mag'] >= 3.5:
            x.append(datetime.datetime.fromtimestamp(p['time']/1000.0 ))
            y.append(p['mag'])  

    x.reverse()
    y.reverse()
    
    # x = x[:10]
    # y = y[:10]

    shortx = [] 
    for i in x:
        shortx.append(i.strftime("%m-%d %H:%M"))
        
        print(x)

    # return render_template('index.html', animal='perro',barData =json.dumps ({ 'x': [1,2,3,4,5,6,7,8], 'y': [23, 45, 55, 20, 10, 80, 100, 45]} ) )
   
    return render_template('index.html', animal='perro',barData =json.dumps ({ 'x': shortx, 'y': y} ) )

@blueprint.route('/<template>')
def route_template(template):

    if not current_user.is_authenticated:
        return redirect(url_for('base_blueprint.login'))

    try:

        return render_template(template + '.html')

    except TemplateNotFound:
        return render_template('page-404.html'), 404
    
    except:
        return render_template('page-500.html'), 500
