from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView, ListAPIView
from rest_framework import permissions

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

from reqman.apps.reqtool.models.suffix_req import Suffix
from reqman.apps.reqtool.rest_api.serializers.suffix_req import SuffixSerializer

from reqman.apps.permissions import IsOwnerOrReadOnly
from reqman.apps.reqtool.rest_api.services import fuseki

from rest_framework.response import Response
from rest_framework.views import APIView

#from reqman.apps.reqtool.models.suffix_req import S_CHOICES, NUMBER_UNITS_CHOICES, TIME_UNITS_CHOICES, FLOW_CHOICES
#from reqman.apps.reqtool.models.suffix_req import NUMERICAL_AFFIRMATIVE_CHOICES, S2_CHOICES, S3_CHOICES, S4_CHOICES, S5_6_CHOICES
from reqman.apps.reqtool.models.requirements import BoilerplateData


class SuffixListAPIView(ListAPIView):
    """
    API view to retrieve list of posts or create new
    """
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]
    serializer_class = SuffixSerializer
    #
    queryset = Suffix.objects.all()

    def get_queryset(self):
        group_pk = self.kwargs['boilerplate']
        #print(group_pk)
        queryset_suffix = Suffix.objects.filter(boilerplate_of_suffix = group_pk)
        return queryset_suffix

    #Custom actions when POST
    def perform_create(self, serializer):
        serializer.save()
        #print the data of the post
        #write_fuseki = fuseki.FusekiActions()
        #write_fuseki.write(serializer.data)

class SuffixDetailsAPIView(RetrieveUpdateDestroyAPIView):
    """
    API view to retrieve, update or delete post
    """
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]

    serializer_class = SuffixSerializer
    
    def get_queryset(self):
        group_pk = self.kwargs['boilerplate']
        #print(group_pk)
        queryset_suffix = Suffix.objects.filter(boilerplate_of_suffix = group_pk)
        return queryset_suffix

    #Custom actions when PUT
    def perform_update(self, serializer):
        instance = serializer.validated_data
        #add data in thn Model BoilerplateData
        flow_instance, num_instance, mumerical_units_instance = [[""], [""], [""]]
        eV_instance = instance['s_choices'].rsplit(':', 1)
        if instance['flow']:
            flow_instance = instance['flow'].rsplit(':', 1)
        if instance['numerical']:
            num_instance = instance['numerical'].rsplit(':', 1)
        if instance['mumerical_units']:
            mumerical_units_instance = instance['mumerical_units'].rsplit(':', 1)
        sf = (eV_instance[len(eV_instance)-1]  + ' ' + num_instance[len(num_instance)-1] + ' ' + mumerical_units_instance[len(mumerical_units_instance)-1] + ' ' + flow_instance[len(flow_instance)-1])
        BoilerplateData.objects.filter(owner_data=instance['boilerplate_of_suffix'].owner, boilerplate_data_id = instance['boilerplate_of_suffix']).update(suffix_data = sf )
        ssuffix_choice = self.getclassofsuffix(instance["s_choices"])
        serializer.save(suffix_choices = ssuffix_choice)
        #print(self.new.changed_data)
        #instance = serializer.save()
        #write_fuseki = fuseki.FusekiActions()
        #write_fuseki.write(serializer.data)

    #Custom actions when DELETE
    def perform_destroy(self, instance):
        instance = serializer.validated_data
        #add data in thn Model BoilerplateData
        #print("deleted")
        instance.delete()
    
    def getclassofsuffix(self, suffix):
        return ("S1")


