from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse, JsonResponse
from rest_framework.parsers import JSONParser

from .models import CarReservations
from .serializers import CarReservationsSerializer

import datetime

TIMEZONE_TIME_DIFF = 2

@csrf_exempt
def reserve_car(request):
    # Getting the reservation request JSON
    reservation_data = JSONParser().parse(request)

    # Creating a start date and end date objects
    reservation_start_datetime = datetime.datetime.strptime(reservation_data['start_date'],"%Y-%m-%dT%H:%M") - datetime.timedelta(hours=TIMEZONE_TIME_DIFF) # Hours for considering timezone
    reservation_end_datetime = datetime.datetime.strptime(reservation_data['end_date'],"%Y-%m-%dT%H:%M") - datetime.timedelta(hours=TIMEZONE_TIME_DIFF) # Hours for considering timezone

    # Making sure there are no collisions
    start_colliding_reservations = CarReservations.objects.filter(start_date__lte=reservation_start_datetime, end_date__gte=reservation_start_datetime, car=reservation_data["car"])
    end_colliding_reservations = CarReservations.objects.filter(start_date__lte=reservation_end_datetime, end_date__gte=reservation_end_datetime, car=reservation_data["car"])
    
     # In case there is a collision with someone else's reservation
    if (start_colliding_reservations.count() != 0 or end_colliding_reservations.count() != 0):
        if (start_colliding_reservations.count() != 0):
            name = start_colliding_reservations[0].name
        else:
            name = end_colliding_reservations[0].name
        return HttpResponse(name + " already uses your car :(", status="409")
    
    # If there is no collision - save this reservation
    reservation = CarReservations(name = reservation_data["name"], car = reservation_data["car"], start_date=reservation_start_datetime, end_date=reservation_end_datetime, destination=reservation_data["destination"])
    reservation.save()

    return HttpResponse(status="200")

def get_reservations(request):
    all_reservations = CarReservations.objects.filter(start_date__gte=datetime.datetime.today() - datetime.timedelta(days=1))
    print (all_reservations)
    reservations_serializer = CarReservationsSerializer(all_reservations, many=True)
    return JsonResponse(reservations_serializer.data, safe=False)

@csrf_exempt
def delete_reservation(request):
    # Getting the reservation request JSON
    reservation_data = JSONParser().parse(request)

    # Creating a start date and end date objects
    try: # In case of deletion of an older reservation (after refreshing the page)
        reservation_start_datetime = datetime.datetime.strptime(reservation_data['start_date'],"%Y-%m-%dT%H:%M:%Sz") - datetime.timedelta(hours=TIMEZONE_TIME_DIFF) # Hours for considering timezone
        reservation_end_datetime = datetime.datetime.strptime(reservation_data['end_date'],"%Y-%m-%dT%H:%M:%Sz") - datetime.timedelta(hours=TIMEZONE_TIME_DIFF) # Hours for considering timezone
    except: # In case of deletion of a newly added reservation (without refreshing the page)
        reservation_start_datetime = datetime.datetime.strptime(reservation_data['start_date'],"%Y-%m-%dT%H:%M") - datetime.timedelta(hours=TIMEZONE_TIME_DIFF) # Hours for considering timezone
        reservation_end_datetime = datetime.datetime.strptime(reservation_data['end_date'],"%Y-%m-%dT%H:%M") - datetime.timedelta(hours=TIMEZONE_TIME_DIFF) # Hours for considering timezone

    # Searching for this reservation
    start_colliding_reservations = CarReservations.objects.filter(start_date=reservation_start_datetime, end_date=reservation_end_datetime, car=reservation_data["car"])
    if (start_colliding_reservations.count() == 0):
        return HttpResponse("Could not find you reservation to delete.", status="404")

    start_colliding_reservations.delete()
    return HttpResponse(status="200")

@csrf_exempt
def update_reservation(request):
    return HttpResponse(status="200")
