from rest_framework import viewsets
import pandas as pd
from lungmap_sparql_client.lungmap_sparql_client import LMClient
from lungmap_sparql_client.lungmap_sparql_utils import get_lungmap_file_list_all
from analytics.models import Experiment
from analytics.serializers import ExperimentSerializer
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status


class LungmapExperimentViewSet(viewsets.ViewSet):
    """
    Utilizing the lungmap_sparql_client, this View calls out to the Lungmap mothership (via SPARQL) to get a list of all
    images, and associated data. From that point, it deduplicates experiment ids and provides a list to the user. 
    """
    def list(self, request):
        alldata_df = pd.DataFrame(LMClient.create_image_table(get_lungmap_file_list_all))
        exp_names_df = alldata_df[['experiment_id']].drop_duplicates()
        return Response(exp_names_df)


class ExperimentList(APIView):
    """
    List all experiments, or create a new experiment.
    """
    def get(self, request, format=None):
        experiments = Experiment.objects.all()
        serializer = ExperimentSerializer(experiments, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = ExperimentSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class ExperimentDetail(APIView):
    """
    Retrieve, update or delete an experiment instance.
    """
    def get_object(self, pk):
        try:
            return Experiment.objects.get(pk=pk)
        except Experiment.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        experiment = self.get_object(pk)
        serializer = ExperimentSerializer(experiment)
        return Response(serializer.data)

    def put(self, request, pk, format=None):
        snippet = self.get_object(pk)
        serializer = ExperimentSerializer(snippet, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        snippet = self.get_object(pk)
        snippet.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

@@ -441,16 +441,34 @@
 t status
+, mixins, generics
 %0A%0A%0Aclass
@@ -1015,16 +1015,105 @@
 entList(
+%0A        mixins.ListModelMixin,%0A        mixins.CreateModelMixin,%0A        generics.Generic
 APIView)
@@ -1188,68 +1188,21 @@
 %22%22%22%0A
-    def get(self, request, format=None):%0A        experiments
+%0A    queryset
  = E
@@ -1225,20 +1225,16 @@
 s.all()%0A
-    
     seri
@@ -1231,32 +1231,38 @@
 )%0A    serializer
+_class
  = ExperimentSer
@@ -1272,73 +1272,8 @@
 izer
-(experiments, many=True)%0A        return Response(serializer.data)
 %0A%0A  
@@ -1282,11 +1282,10 @@
 def 
-pos
+ge
 t(se
@@ -1301,301 +1301,175 @@
 st, 
-format=None):%0A        serializer = ExperimentSerializer(data=request.data)%0A        if serializer.is_valid():%0A            serializer.save()%0A            return Response(serializer.data, status=status.HTTP_201_CREATED)%0A        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST
+*args, **kwargs):%0A        return self.list(request, *args, **kwargs)%0A%0A    def post(self, request, *args, **kwargs):%0A        return self.create(request, *args, **kwargs
 )%0A%0A%0A

change ExperimentList to a GenericAPIView so browsable docs show experiment_id form field