from __future__ import absolute_import, division, print_function, unicode_literals

from django.db.models import Max, Q

from rest_framework import exceptions, status
from rest_framework.decorators import detail_route
from rest_framework.response import Response

from resolwe.flow.models import Collection
from resolwe.flow.views import CollectionViewSet
from .filters import SampleFilter
from .models import Sample
from .serializers import SampleSerializer


class SampleViewSet(CollectionViewSet):
    filter_class = SampleFilter
    serializer_class = SampleSerializer

    def get_queryset(self):
        queryset = Sample.objects.annotate(
            latest_date=Max('data__created')
        ).prefetch_related('descriptor_schema')

        annotated = self.request.query_params.get('annotated', None)

        if annotated == "1":
            queryset = queryset.filter(Q(descriptor__has_key='geo') & Q(descriptor__geo__has_key='annotator'))
        elif annotated == "0":
            queryset = queryset.filter(~Q(descriptor__has_key='geo') | ~Q(descriptor__geo__has_key='annotator'))

        return queryset.order_by('-latest_date')

    @detail_route(methods=[u'post'])
    def add_to_collection(self, request, pk=None):
        sample = self.get_object()

        if 'ids' not in request.data:
            return Response({"error": "`ids`parameter is required"}, status=status.HTTP_400_BAD_REQUEST)

        for collection_id in request.data['ids']:
            collection_query = Collection.objects.filter(pk=collection_id)
            if not collection_query.exists():
                raise exceptions.ValidationError('Collection id does not exist')
            collection = collection_query.first()
            if not request.user.has_perm('add_collection', obj=collection):
                if request.user.is_authenticated():
                    raise exceptions.PermissionDenied()
                else:
                    raise exceptions.NotFound()

        for collection_id in request.data['ids']:
            sample.collections.add(collection_id)

        return Response()

    @detail_route(methods=[u'post'])
    def remove_from_collection(self, request, pk=None):
        sample = self.get_object()

        if 'ids' not in request.data:
            return Response({"error": "`ids`parameter is required"}, status=status.HTTP_400_BAD_REQUEST)

        for collection_id in request.data['ids']:
            collection_query = Collection.objects.filter(pk=collection_id)
            if not collection_query.exists():
                raise exceptions.ValidationError('Collection id does not exist')
            collection = collection_query.first()
            if not request.user.has_perm('add_collection', obj=collection):
                if request.user.is_authenticated():
                    raise exceptions.PermissionDenied()
                else:
                    raise exceptions.NotFound()

        for collection_id in request.data['ids']:
            sample.collections.remove(collection_id)

        return Response()

@@ -803,16 +803,67 @@
  None)%0A%0A
+        # Return annotated samples only by default%0A
         
@@ -883,9 +883,9 @@
 == %22
-1
+0
 %22:%0A 
@@ -922,16 +922,17 @@
 .filter(
+~
 Q(descri
@@ -956,10 +956,11 @@
 o') 
-& 
+%7C ~
 Q(de
@@ -1006,35 +1006,18 @@
       el
-if annotated == %220%22
+se
 :%0A      
@@ -1041,33 +1041,32 @@
 queryset.filter(
-~
 Q(descriptor__ha
@@ -1070,35 +1070,34 @@
 _has_key='geo') 
-%7C ~
+& 
 Q(descriptor__ge

Return annotated samples only by default