from django.http import JsonResponse
from django.core import serializers
from django.shortcuts import render
from django.views.generic import ListView, TemplateView
from .models import ArduinoPrueba, Integrante, Publicacion
import json
from datetime import timezone
import pytz


class Index(TemplateView):
    template_name = "index.html"


class Historia(TemplateView):
    template_name = "historia.html"


class IntegrantesListView(ListView):
    model = Integrante
    template_name = "integrantes.html"


class Arboles(TemplateView):
    template_name = "arboles.html"


class Servicios(TemplateView):
    template_name = "servicios.html"


class Publicaciones(TemplateView):
    template_name = "publicaciones.html"


class PublicacionListView(ListView):
    model = Publicacion
    template_name = "publicaciones.html"


class Notas(TemplateView):
    template_name = "notas.html"


class Contacto(TemplateView):
    template_name = "contacto.html"


class ArduinoPruebaListView(ListView):
    # temporalmente
    model = ArduinoPrueba
    # queryset = ArduinoPrueba.objects.all().order_by('-id')[:4]
    # queryset = ArduinoPrueba.objects.all().order_by('-id')[:4][::-1]
    template_name = "arduino_prueba.html"


class Arduino(TemplateView):
    template_name = "arduino.html"


def chart_data(request):
    # temp = ArduinoPrueba.objects.all().values_list('temp', flat=True)[6::3]
    temp = [x for x in reversed(ArduinoPrueba.objects.all().order_by('-fecha').values_list('temp', flat=True)[:10])]
    # fechas = ArduinoPrueba.objects.all().values_list('fecha', flat=True)[6::3]
    # fechas = [x for x in reversed(ArduinoPrueba.objects.all().order_by('-id').values_list('fecha', flat=True)[:10])]
    fechas = [x[0].astimezone(pytz.timezone('America/Argentina/Jujuy')).strftime('%m-%d %H:%M') for x in reversed(ArduinoPrueba.objects.all().order_by('-fecha').values_list('fecha')[:10])]
    # hum = ArduinoPrueba.objects.all().values_list('hum', flat=True)[6::3]
    hum = [x for x in reversed(ArduinoPrueba.objects.all().order_by('-fecha').values_list('hum', flat=True)[:10])]
    # print('temp: {}'.format(len(temp)))
    # print('fechas: {}'.format(len(fechas)))
    # print('hum: {}'.format(len(hum)))
    # longitud = list(range(len(temp)))
    # longitud = [str(x) for x in longitu]
    series = {
        # 'type': 'bar',
        # 'xAxis': {'categories': ['Apples', 'Bananas', 'Oranges']},
        'xAxis': {'categories': fechas},
        'title': {'text': 'Temperatura Arduino DHT22  2019'},
        'legend': {
        'align': 'left',
        'verticalAlign': 'top',
        'borderWidth': 0},
        'plotOptions': {
            'line':{
                'dataLabels':{'enabled': True},
                'enableMouseTracking': False,
            }
        },
        'yAxis': {
        'title': {
            'text': None
        }
    },
        'series': [
        #     {
        #     'name': 'Humedad',
        #     # 'color': '#940315',
        #     'color': '#3164CA',
        #     'data': hum
        # },
        {
            'name': 'Temperatura',
            # 'data': [3, 0, 8]
            'color': '#DC3912',
            'data': temp
        }
        ]
        }
    # json_r = serializers.serialize('json', dataset)
    # return JsonResponse(json_r, content_type='application/json', safe=False)
    return JsonResponse(series)
