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 apis.serializer import AdditionalChargesSerializers
from authentication.auth import verify_token

from .models import Daytimes, RateLists, AdditionalCharges, Taxes
from .serializer import AdditionalChargesSerializer, DayTimesSerializer,RateListsSerailizer, TaxesSerializer


@csrf_exempt
def rateLists(request):
    user_id = verify_token(request)
    if user_id == None:
        return JsonResponse({'message' : "User not logged in.", 'status':status.HTTP_401_UNAUTHORIZED}, safe=False, status=status.HTTP_401_UNAUTHORIZED)
    else:           
        if request.method == 'POST':
            request_data = json.loads(request.body)
            bulk_list = []
            for rate in request_data['rates']:
                for price in rate['prices']:
                    rate_list = RateLists()
                    
                    rate_list.uniform_type = rate['uniform_type']
                    rate_list.station_type = rate['station_type']
                    rate_list.trip_type = rate['trip_type']
                    rate_list.day = rate['day']
                    rate_list.duration = price['minutes']
                    rate_list.rate = price['price']
                    bulk_list.append(rate_list)
                    # rate_list.save()
            RateLists.objects.bulk_create(bulk_list)
            return JsonResponse({'message': "RateList added successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)   
        else:   
            rate = RateLists.objects.all()
            serializer = RateListsSerailizer(rate, many=True)
            return JsonResponse({'ratelists': serializer.data, 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        

    
@csrf_exempt
def editRateList(request):
    if request.method == 'POST':
        request_data = json.loads(request.body)
        if request_data['id']:
            rate = RateLists.objects.get(id=request_data['id'])
            rate.day = int(request_data['day'])
            rate.duration = request_data['duration']
            rate.uniform_type = int(request_data['uniform_type'])
            rate.station_type = int(request_data['station_type'])
            rate.trip_type = int(request_data['trip_type'])
            rate.rate = request_data['rate']
            rate.save()
            return JsonResponse({'message' : "RateList edited Succesfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        else:
            return JsonResponse({'message': "Ratelist id is required.", 'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
    else:
        if request.GET.get('id'):
            try:
                rate_list = RateLists.objects.get(id=request.GET.get('id'))
                serializer = RateListsSerailizer(rate_list, many=False)
                return JsonResponse({'message': serializer.data, 'status':status.HTTP_200_OK},safe=False,status=status.HTTP_200_OK)
            except RateLists.DoesNotExist:
                return JsonResponse({'message':"RateList doesn't exist", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
        else:
            return JsonResponse({'message': "Ratelist id is required.", 'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
        


@csrf_exempt
def deleteRateList(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)        
            RateLists.objects.get(id = request_data['id']).delete()
            return JsonResponse({'message': "Rate deleted Succesfully", 'status': status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        except RateLists.DoesNotExist:
            return JsonResponse({'message':"Ratelist doesn't exists. Invalid ratelist id", 'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)



@csrf_exempt
def additionalCharges(request):
    user_id = verify_token(request)
    if user_id == None:
        return JsonResponse({'message' : "User not logged in.", 'status':status.HTTP_401_UNAUTHORIZED}, safe=False, status=status.HTTP_401_UNAUTHORIZED)
    else:
        if request.method == 'POST':
            request_data = json.loads(request.body)
            additional_charge = AdditionalCharges()
            additional_charge.title = request_data['title']
            additional_charge.price = request_data['price']
            additional_charge.save()
            return JsonResponse({'message' : "Additional Charges added Succesfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        else:
            additional_charge = AdditionalCharges.objects.all()            
            serializer = AdditionalChargesSerializer(additional_charge, many=True)
            return JsonResponse({'additional_charges': serializer.data, 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        

   
@csrf_exempt
def taxes(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)
            tax = Taxes()
            tax.title = request_data['title']
            tax.tax_percentage = request_data['tax_percentage']
            tax.save()
            return JsonResponse({'message':"Tax added successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        else:
            tax = Taxes.objects.all()
            serializer = TaxesSerializer(tax, many=True)
            return JsonResponse({'taxes':serializer.data, 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        

        
@csrf_exempt
def editTax(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)
            if request_data['id']:
                tax = Taxes.objects.get(id = request_data['id'])
                tax.title = request_data['title']
                tax.tax_percentage = request_data['tax_percentage']
                tax.save()
                return JsonResponse({'message':"Tax edited successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
            else:
                return JsonResponse({'message':"Tax id is required", 'status':status.HTTP_400_BAD_REQUEST},safe=False, status=status.HTTP_400_BAD_REQUEST)
            
        else:
            if request.GET.get('id'):
                try:
                    tax = Taxes.objects.get(id=request.GET.get('id'))
                    serializer = TaxesSerializer(tax, many=False)
                    return JsonResponse({'tax':serializer.data, 'status':status.HTTP_200_OK},safe=False,status=status.HTTP_200_OK)
                except Taxes.DoesNotExist:
                    return JsonResponse({'message':"Tax doesn't exist", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
            else:
                return JsonResponse({'message': "Tax id is required.", 'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
            


@csrf_exempt        
def deleteTax(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)
            Taxes.objects.get(id = request_data['id']).delete()
            return JsonResponse({'message':"Tax deleted successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        except Taxes.DoesNotExist:
            return JsonResponse({'message': "Tax id is required", 'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
        


@csrf_exempt
def dayTimes(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:
        dayTime = Daytimes.objects.all()
        serializer = DayTimesSerializer(dayTime, many=True)
        return JsonResponse({'dayTimes':serializer.data, 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)

    

@csrf_exempt
def editDayTime(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)
            if request_data['id']:
                day_time = Daytimes.objects.get(id=request_data['id'])
                day_time.day_type = int(request_data['day_type'])
                day_time.start_time = request_data['start_time']
                day_time.end_time = request_data['end_time']
                day_time.save()
                return JsonResponse({'message':"DayType & Time edited successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
            else:
                return JsonResponse({'message':"Day & Time id is required ", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
        else:
            if request.GET.get('id'):
                try:
                    day_time = Daytimes.objects.get(id = request.GET.get('id'))
                    serializer = DayTimesSerializer(day_time, many=False)
                    return JsonResponse({'day_time':serializer.data, 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
                except Daytimes.DoesNotExist:
                    return JsonResponse({'message':"Day&Time doesn't exist", 'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
            else:
                return JsonResponse({'message':"Day&Time id is required.", 'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)





