# Path: transappbusposition/models.py
# class Ultimo15MinGps(models.Model):
#     patente = models.CharField(max_length=7, blank=True, null=True)
#     servicio = models.CharField(max_length=50, blank=True, null=True)
#     tiempo = models.DateTimeField(blank=True, null=True)
#     latitud = models.FloatField(blank=True, null=True)
#     longitud = models.FloatField(blank=True, null=True)
#     dist_en_ruta = models.IntegerField(blank=True, null=True)
#     dist_a_ruta = models.IntegerField(blank=True, null=True)
#     velocidad_instantanea = models.IntegerField(blank=True, null=True)
#     operador = models.IntegerField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'ultimo_15min_gps'


from django.http import JsonResponse
from django.shortcuts import render
from django.views.generic import View
from django.utils import timezone, dateparse
from transappbusposition.models import Ultimo15MinGps
import datetime

# model

class GetEstimatedBusPosition(View):
    ''' This class requests to the database the bus position based on date '''

    #def __init__(self):
    #	self.context={}

    def get(self, request):
        """ it gives the nearest GPS record respect to time for a bus """

        licencePlate = request.GET['licencePlate']
        fLicencePlate = licencePlate[:4] + '-' + licencePlate[-2:]
        time = request.GET['time']

        #licencePlate = "CJRB-73"
        #time = "2016-11-12T15:58:00"
        # debo pasarle la hora con 3 horas menos para que matchee
        time = dateparse.parse_datetime(time)
        offsetTime = time - datetime.timedelta(hours=3)
        #time = timezone.make_aware(time)
        
        response = {}
        response['error'] = False

        closestGreaterQS = Ultimo15MinGps.objects.filter(tiempo__gte=offsetTime, patente=fLicencePlate).order_by('tiempo')

# Path: transappbusposition/models.py
# class Ultimo15MinGps(models.Model):
#     patente = models.CharField(max_length=7, blank=True, null=True)
#     servicio = models.CharField(max_length=50, blank=True, null=True)
#     tiempo = models.DateTimeField(blank=True, null=True)
#     latitud = models.FloatField(blank=True, null=True)
#     longitud = models.FloatField(blank=True, null=True)
#     dist_en_ruta = models.IntegerField(blank=True, null=True)
#     dist_a_ruta = models.IntegerField(blank=True, null=True)
#     velocidad_instantanea = models.IntegerField(blank=True, null=True)
#     operador = models.IntegerField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'ultimo_15min_gps'


# Path: transappbusposition/views.py
from django.http import JsonResponse
from django.shortcuts import render
from django.views.generic import View
from django.utils import timezone, dateparse
from transappbusposition.models import Ultimo15MinGps
import datetime


# model

class GetEstimatedBusPosition(View):
    ''' This class requests to the database the bus position based on date '''

    #def __init__(self):
    #	self.context={}

    def get(self, request):
        """ it gives the nearest GPS record respect to time for a bus """

        licencePlate = request.GET['licencePlate']
        fLicencePlate = licencePlate[:4] + '-' + licencePlate[-2:]
        time = request.GET['time']

        #licencePlate = "CJRB-73"
        #time = "2016-11-12T15:58:00"
        # debo pasarle la hora con 3 horas menos para que matchee
        time = dateparse.parse_datetime(time)
        offsetTime = time - datetime.timedelta(hours=3)
        #time = timezone.make_aware(time)
        
        response = {}
        response['error'] = False

        closestGreaterQS = Ultimo15MinGps.objects.filter(tiempo__gte=offsetTime, patente=fLicencePlate).order_by('tiempo')

        closestLessQS    = Ultimo15MinGps.objects.filter(tiempo__lt=offsetTime, patente=fLicencePlate).order_by('-tiempo')