from django.shortcuts import render
from rest_framework import status
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
import json
from authentication.auth import verify_token
from vehicles.serializers import TransmissionTypesSerializer, VehicleModelsSerializer, VehicleTypesSerializer
from .models import TransmissionTypes, VehicleModels, VehicleTypes

# Create your views here.

@csrf_exempt
def vehicleTypes(request):
    user_id = verify_token(request)
    if user_id == None:
        return JsonResponse ({'message':"User is not login", 'status':status.HTTP_401_UNAUTHORIZED}, safe=False, status=status.HTTP_401_UNAUTHORIZED)
    
    else:
        if request.method == 'POST':
            request_data = json.loads(request.body)
            vehicle_type = VehicleTypes()
            vehicle_type.name = request_data['name']
            vehicle_type.save()
            return JsonResponse({'message': "Vehicle type added successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        
        else:
            vehicle_type = VehicleTypes.objects.all()
            serializer = VehicleTypesSerializer(vehicle_type, many=True)
            return JsonResponse({'vehicle_types': serializer.data, 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)

@csrf_exempt
def editVehicleType(request):
    if request.method == 'POST':
        request_data = json.loads(request.body)
        if request_data['id']:
            vehicle_type = VehicleTypes.objects.get(id = request_data['id'])
            vehicle_type.name = request_data['name']
            vehicle_type.save()
            return JsonResponse({'message': "Vehicle type Edited successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        else:
         return JsonResponse({'message':"Vehicle type id is required", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)        

    else:
        if request.GET.get('id'):
            try:
                vehicle_type = VehicleTypes.objects.get(id = request.GET.get('id'))
                serializer = VehicleTypesSerializer(vehicle_type, many=False)
                return JsonResponse({'vehicle_type': serializer.data,'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)   
            except VehicleTypes.DoesNotExist:
                return JsonResponse({'message':"Vehicle type doesn't exist", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
        else:
            return JsonResponse({'message':"Vehicle type id is required", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)        

                

@csrf_exempt
def deleteVehicleType(request):
    user_id = verify_token(request)
    if user_id == None:
        return JsonResponse({'message': "User not login.", 'status':status.HTTP_401_UNAUTHORIZED}, safe=False, status=status.HTTP_401_UNAUTHORIZED)
    else:
        try:
            request_data = json.loads(request.body)
            VehicleTypes.objects.get(id = request_data['id']).delete()
            return JsonResponse({'message':"Vehicle Type delete Successfully", 'status':status.HTTP_200_OK})
        except VehicleTypes.DoesNotExist:
            return JsonResponse({'message':"VehicleTypes doesn't exists, Invalid VehicleTypes id ", 'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)


@csrf_exempt
def vehicleModels(request):
    user_id = verify_token(request)
    if user_id == None:
        return JsonResponse ({'message':"User is not login", 'status':status.HTTP_401_UNAUTHORIZED}, safe=False, status=status.HTTP_401_UNAUTHORIZED)
    
    else:
        if request.method == 'POST':
            request_data = json.loads(request.body)
            vehicle_model = VehicleModels()
            vehicle_model.name = request_data['name']
            vehicle_model.save()
            return JsonResponse({'message': "Vehicle model added successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        
        else:
            vehicle_model = VehicleModels.objects.all()
            serializer = VehicleModelsSerializer(vehicle_model, many=True)
            return JsonResponse({'vehicle_models': serializer.data, 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)

@csrf_exempt
def editVehicleModel(request):
    if request.method == 'POST':
        request_data = json.loads(request.body)
        if request_data['id']:
            vehicle_model = VehicleModels.objects.get(id = request_data['id'])
            vehicle_model.name = request_data['name']
            vehicle_model.save()
            return JsonResponse({'message': "Vehicle model Edited successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        else:
            return JsonResponse({'message':"Vehicle model id is required", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)        

    else:
        if request.GET.get('id'):
            try:
                vehicle_model = VehicleModels.objects.get(id = request.GET.get('id'))
                serializer = VehicleModelsSerializer(vehicle_model, many=False)
                return JsonResponse({'vehicle_model':serializer.data,'status':status.HTTP_200_OK},safe=False,status=status.HTTP_200_OK)
            except VehicleModels.DoesNotExist:
                return JsonResponse({'message': " Vehicle model doesn't exist",'status':status.HTTP_401_UNAUTHORIZED}, safe=False, status=status.HTTP_401_UNAUTHORIZED)
        else:
             return JsonResponse({'message':"Vehicle model id is required", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)        
  
        
@csrf_exempt
def deleteVehicleModel(request):
    user_id = verify_token(request)
    if user_id == None:
        return JsonResponse({'message': "User not login.", 'status':status.HTTP_401_UNAUTHORIZED}, safe=False, status=status.HTTP_401_UNAUTHORIZED)
    else:
        try:
            request_data = json.loads(request.body)
            VehicleModels.objects.get(id = request_data['id']).delete()
            return JsonResponse({'message':"Vehicle Model delete Successfully", 'status':status.HTTP_200_OK})
        except VehicleModels.DoesNotExist:
            return JsonResponse({'message':"Vehicle model id is required", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)        
  


@csrf_exempt
def transmissionTypes(request):
    user_id = verify_token(request)
    if user_id == None:
        return JsonResponse({'message': "User not login.", 'status':status.HTTP_401_UNAUTHORIZED}, safe=False, status=status.HTTP_401_UNAUTHORIZED)
    else:       
        transmission_type = TransmissionTypes.objects.all()
        serializer = TransmissionTypesSerializer(transmission_type, many=True)
        return JsonResponse({'transmission_types': serializer.data, 'status':status.HTTP_200_OK}, safe=False)

@csrf_exempt
def editTransmissionType(request):
    if request.method == 'POST':
        request_data = json.loads(request.body)
        if request_data['id']:
            transmission_type = TransmissionTypes.objects.get(id = request_data['id'])
            transmission_type.name = request_data['name']
            transmission_type.save()
            return JsonResponse({'message': "Transmission  Type Edited successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        else:
            return JsonResponse({'message':" Transmission type id is required", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)        

    else:
        if request.GET.get('id'):
            try:
               transmission_type = TransmissionTypes.objects.get(id = request.GET.get('id'))
               serializer = TransmissionTypesSerializer(transmission_type, many=False)
               return JsonResponse({'transmission_type':serializer.data, 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK) 
            except TransmissionTypes.DoesNotExist:
                return JsonResponse({'message': "Transmission type doesn't exist",'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)   
        else:
            return JsonResponse({'message':" Transmission type id is required", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
        

@csrf_exempt
def deleteTransmissionType(request):
    user_id  =  verify_token(request)
    if user_id == None:
        return JsonResponse({'message': "user is not login.", 'status':status.HTTP_401_UNAUTHORIZED}, safe=False, status=status.HTTP_401_UNAUTHORIZED)
    else:
        try:
            request_data = json.loads(request.body)
            TransmissionTypes.object.get(id=request_data['id']).delete()
            return JsonResponse({'message':"Transmission type deleted successfully",'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        except TransmissionTypes.DoesNotExist:
            return JsonResponse({'message':"Transmission type doesn't exist.",'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)


             