from django.shortcuts import render

# Create your views here.
from rest_framework import generics
from rest_framework import permissions
from rest_framework.response import Response
from rest_framework.views import status

from .decorators import validate_request_driver_data

from .models import Driver, Kart, Params, Relay
from .serializers import DriverSerializer, RelaySerializer, ParamSerializer, KartSerializer, KartSimpleSerializer

import logging
logger = logging.getLogger(__name__)


class DriversDetailView(generics.ListAPIView):
    """
    Provides a get method handler.
    """
    queryset = Driver.objects.all()
    serializer_class = DriverSerializer
    #permission_classes = (permissions.IsAuthenticated,)

    def get(self, request, *args, **kwargs):
        try:
            a_song = self.queryset.get(pk=kwargs["pk"])
            return Response(DriverSerializer(a_song).data)
        except Driver.DoesNotExist:
            return Response(
                data={
                    "message": "Driver with id: {} does not exist".format(kwargs["pk"])
                },
                status=status.HTTP_404_NOT_FOUND
            )

    #@validate_request_driver_data
    def put(self, request, *args, **kwargs):
        try:
            a_driver = self.queryset.get(pk=kwargs["pk"])
            serializer = DriverSerializer(instance=a_driver, data=request.data, partial=True)
            updated_driver = serializer.update(a_driver, request.data)
            return Response(DriverSerializer(updated_driver).data)
        except Driver.DoesNotExist:
            return Response(
                data={
                    "message": "Driver with id: {} does not exist".format(kwargs["pk"])
                },
                status=status.HTTP_404_NOT_FOUND
            )

    def delete(self, request, *args, **kwargs):
        try:
            a_driver = self.queryset.get(pk=kwargs["pk"])
            a_driver.delete()
            return Response(status=status.HTTP_204_NO_CONTENT)
        except Driver.DoesNotExist:
            return Response(
                data={
                    "message": "Driver with id: {} does not exist".format(kwargs["pk"])
                },
                status=status.HTTP_404_NOT_FOUND
            )


class KartsDetailView(generics.ListAPIView):
    """
    Provides a get method handler.
    """
    queryset = Kart.objects.all()
    serializer_class = KartSerializer
    #permission_classes = (permissions.IsAuthenticated,)

    def get(self, request, *args, **kwargs):
        try:
            a_song = self.queryset.get(pk=kwargs["pk"])
            return Response(KartSerializer(a_song).data)
        except Kart.DoesNotExist:
            return Response(
                data={
                    "message": "Kart with id: {} does not exist".format(kwargs["pk"])
                },
                status=status.HTTP_404_NOT_FOUND
            )

    #@validate_request_driver_data
    def put(self, request, *args, **kwargs):
        try:
            a_driver = self.queryset.get(pk=kwargs["pk"])
            serializer = KartSerializer(instance=a_driver, data=request.data, partial=True)
            updated_driver = serializer.update(a_driver, request.data)
            return Response(KartSerializer(updated_driver).data)
        except Kart.DoesNotExist:
            return Response(
                data={
                    "message": "Kart with id: {} does not exist".format(kwargs["pk"])
                },
                status=status.HTTP_404_NOT_FOUND
            )

    def delete(self, request, *args, **kwargs):
        try:
            a_driver = self.queryset.get(pk=kwargs["pk"])
            a_driver.delete()
            return Response(status=status.HTTP_204_NO_CONTENT)
        except Kart.DoesNotExist:
            return Response(
                data={
                    "message": "Kart with id: {} does not exist".format(kwargs["pk"])
                },
                status=status.HTTP_404_NOT_FOUND
            )


class ParamsDetailView(generics.ListAPIView):
    """
    Provides a get method handler.
    """
    queryset = Params.objects.all()
    serializer_class = ParamSerializer
    #permission_classes = (permissions.IsAuthenticated,)

    def get(self, request, *args, **kwargs):
        try:
            a_param = self.queryset.get(pk=kwargs["pk"])
            return Response(ParamSerializer(a_param).data)
        except Params.DoesNotExist:
            return Response(
                data={
                    "message": "Param with id: {} does not exist".format(kwargs["pk"])
                },
                status=status.HTTP_404_NOT_FOUND
            )

    #@validate_request_driver_data
    def put(self, request, *args, **kwargs):
        try:
            a_param = self.queryset.get(pk=kwargs["pk"])
            serializer = ParamSerializer(instance=a_param, data=request.data, partial=True)
            updated_param = serializer.update(a_param, request.data)
            return Response(ParamSerializer(updated_param).data)
        except Params.DoesNotExist:
            return Response(
                data={
                    "message": "Param with id: {} does not exist".format(kwargs["pk"])
                },
                status=status.HTTP_404_NOT_FOUND
            )


class ListCreateDriversView(generics.ListCreateAPIView):
    """
    GET songs/
    POST songs/
    """
    queryset = Driver.objects.all()
    serializer_class = DriverSerializer
    #permission_classes = (permissions.IsAuthenticated,)

    #@validate_request_driver_data
    def post(self, request, *args, **kwargs):
        logger.error('ListCreateDriversView => post')
        a_song = Driver.objects.create(
            short_name=request.data["short_name"],
            first_name=request.data["first_name"],
            last_name=request.data["last_name"],
            order=request.data["order"],
            ref_time=request.data["ref_time"],
            kart_id=request.data["kart_id"]
        )
        return Response(
            data=DriverSerializer(a_song).data,
            status=status.HTTP_201_CREATED
        )


class ListCreateKartsView(generics.ListCreateAPIView):
    """
    GET songs/
    POST songs/
    """
    queryset = Kart.objects.all()
    serializer_class = KartSerializer

    #@validate_request_driver_data
    def post(self, request, *args, **kwargs):
        a_kart = Kart.objects.create(
            name=request.data["name"],
            autonomy=request.data["autonomy"],
            comment=request.data["comment"]
        )
        return Response(
            data=KartSerializer(a_kart).data,
            status=status.HTTP_201_CREATED
        )


class ListCreateRelaysView(generics.ListCreateAPIView):
    """
    GET songs/
    POST songs/
    """
    queryset = Relay.objects.all()
    serializer_class = RelaySerializer

    #@validate_request_driver_data
    def post(self, request, *args, **kwargs):
        a_kart = Relay.objects.create(
            name=request.data["name"],
            autonomy=request.data["autonomy"],
            comment=request.data["comment"]
        )
        return Response(
            data=RelaySerializer(a_kart).data,
            status=status.HTTP_201_CREATED
        )


class RelaysDetailView(generics.ListAPIView):
    """
    Provides a get method handler.
    """
    queryset = Relay.objects.all()
    serializer_class = RelaySerializer
    #permission_classes = (permissions.IsAuthenticated,)

    def get(self, request, *args, **kwargs):
        try:
            a_relay = self.queryset.get(pk=kwargs["pk"])
            return Response(RelaySerializer(a_relay).data)
        except Relay.DoesNotExist:
            return Response(
                data={
                    "message": "Relay with id: {} does not exist".format(kwargs["pk"])
                },
                status=status.HTTP_404_NOT_FOUND
            )

    #@validate_request_driver_data
    def put(self, request, *args, **kwargs):
        try:
            a_relay = self.queryset.get(pk=kwargs["pk"])
            serializer = RelaySerializer(instance=a_relay, data=request.data, partial=True)
            updated_relay = serializer.update(a_relay, request.data)
            return Response(RelaySerializer(updated_relay).data)
        except Relay.DoesNotExist:
            return Response(
                data={
                    "message": "Relay with id: {} does not exist".format(kwargs["pk"])
                },
                status=status.HTTP_404_NOT_FOUND
            )

    def delete(self, request, *args, **kwargs):
        try:
            a_relay = self.queryset.get(pk=kwargs["pk"])
            a_relay.delete()
            return Response(status=status.HTTP_204_NO_CONTENT)
        except Relay.DoesNotExist:
            return Response(
                data={
                    "message": "Relay with id: {} does not exist".format(kwargs["pk"])
                },
                status=status.HTTP_404_NOT_FOUND
            )


class RelaysByKartDetailView(generics.ListAPIView):
    """
    Provides a get method handler.
    """
    serializer_class = RelaySerializer

    def get_queryset(self):
        try:
            kart = self.kwargs['kart']
            a_relay = Relay.objects.filter(kart=kart)
            return a_relay
        except Relay.DoesNotExist:
            return Response(
                data={
                    "message": "Relay with id: {} does not exist".format(self.kwargs["kart"])
                },
                status=status.HTTP_404_NOT_FOUND
            )
