from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers, viewsets
from rest_framework.decorators import action
from rest_framework.permissions import DjangoModelPermissions
from rest_framework.response import Response

from bananas.admin.api.schemas import schema, schema_serializer_method
from bananas.admin.api.views import BananasAPI
from bananas.compat import reverse
from bananas.lazy import lazy_title


class UserDetailsSerializer(serializers.HyperlinkedModelSerializer):
    full_name = serializers.SerializerMethodField()

    @schema_serializer_method(serializer_or_field=serializers.CharField)
    def get_full_name(self, obj):
        return obj.get_full_name()

    class Meta:
        model = User
        fields = ("id", "url", "username", "full_name", "email", "is_staff")

    def build_url_field(self, field_name, model_class):
        """
        This is needed due to DRF's model serializer uses the queryset to build url name

        # TODO: Move this to own serializer mixin or fix problem elsewhere?
        """
        field, kwargs = super().build_url_field(field_name, model_class)

        view = self.root.context["view"]
        kwargs["view_name"] = view.get_url_name("detail")

        return field, kwargs


class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ("username", "first_name", "last_name", "email")


class UserFilterSerializer(serializers.Serializer):
    username = serializers.CharField(required=False)


class UserViewSet(BananasAPI, viewsets.ModelViewSet):

    name = lazy_title(_("users"))
    permission_classes = (DjangoModelPermissions,)
    queryset = User.objects.all()
    serializer_class = UserSerializer

    class Admin:
        verbose_name = lazy_title(_("user"))

    @schema(query_serializer=UserFilterSerializer)
    def list(self, request):
        return super().list(request)

    def get_queryset(self):
        queryset = super().get_queryset()

        serializer = UserFilterSerializer(data=self.request.query_params)
        serializer.is_valid(raise_exception=True)
        username = serializer.validated_data.get("username")
        if username:
            queryset = queryset.filter(username__contains=username)

        return queryset

    def get_serializer_class(self):
        if self.action in ("retrieve", "list"):
            return UserDetailsSerializer
        else:
            return UserSerializer

    @action(detail=False)
    def foo(self, request):
        return Response("Just a simple extra list action")

    @action(detail=True)
    def bar(self, request, pk):
        url = reverse("bananas:v1.0:example.user-bar", kwargs={"pk": pk})
        return Response(
            "Just a simple extra detail action, url = {url}".format(url=url)
        )

    @action(detail=True, methods=["post"], name=_("Send activation e-mail"))
    def send_activation_email(self, request, pk):
        return Response("Just another simple extra detail action")

    @action(detail=True, url_path="baz/(?P<x>.+)/ham/(?P<y>.+)")
    def baz(self, request, pk, x, y):
        return Response(
            "Just a simple extra detail action, {pk}, {x}, {y}".format(pk=pk, x=x, y=y)
        )

@@ -1587,24 +1587,687 @@
 ed=False)%0A%0A%0A
+class FormSerializer(serializers.Serializer):%0A    text = serializers.CharField()%0A    integer = serializers.IntegerField()%0A    date = serializers.DateField()%0A    datetime = serializers.DateTimeField()%0A    boolean = serializers.BooleanField(required=True)%0A    choices = serializers.ChoiceField(%0A        choices=(%0A            (%22se%22, %22Sweden%22),%0A            (%22no%22, %22Norway%22),%0A            (%22fi%22, %22Finland%22),%0A            (%22dk%22, %22Denmark%22),%0A        )%0A    )%0A    multiple_choices = serializers.MultipleChoiceField(%0A        choices=(%0A            (%22se%22, %22Sweden%22),%0A            (%22no%22, %22Norway%22),%0A            (%22fi%22, %22Finland%22),%0A            (%22dk%22, %22Denmark%22),%0A        )%0A    )%0A%0A%0A
 class UserVi
@@ -2308,16 +2308,16 @@
 ewSet):%0A
-
 %0A    nam
@@ -2431,46 +2431,8 @@
 ll()
-%0A    serializer_class = UserSerializer
 %0A%0A  
@@ -3106,16 +3106,86 @@
 ializer%0A
+        elif self.action == %22form%22:%0A            return FormSerializer%0A
         
@@ -3994,16 +3994,16 @@
 x, y=y)%0A
-
         
@@ -3992,20 +3992,330 @@
 x=x, y=y)%0A        )%0A
+%0A    @action(detail=False, methods=%5B%22get%22, %22post%22%5D)%0A    def form(self, request):%0A        if request.method == %22GET%22:%0A            return Response(%7B%7D)%0A        serializer = FormSerializer(data=self.request.data)%0A        serializer.is_valid(raise_exception=True)%0A        return Response(serializer.validated_data)%0A

Add a dummy view for AutoField testing