"""Notification app common views."""
from django.shortcuts import get_object_or_404
from rest_framework import generics, permissions
from rest_framework.response import Response

from notification import models
from notification.serializers import common as serializers
from utils.methods import get_user_ip


class CreateSubscribeView(generics.CreateAPIView):
    """Create subscribe View."""

    queryset = models.Subscriber.objects.all()
    permission_classes = (permissions.AllowAny,)
    serializer_class = serializers.CreateSubscribeSerializer


class UpdateSubscribeView(generics.UpdateAPIView):
    """Subscribe info view."""

    lookup_field = 'update_code'
    lookup_url_kwarg = 'code'
    permission_classes = (permissions.AllowAny,)
    queryset = models.Subscriber.objects.all()
    serializer_class = serializers.UpdateSubscribeSerializer


class SubscribeInfoView(generics.RetrieveAPIView):
    """Subscribe info view."""

    lookup_field = 'update_code'
    lookup_url_kwarg = 'code'
    permission_classes = (permissions.AllowAny,)
    queryset = models.Subscriber.objects.all()
    serializer_class = serializers.SubscribeSerializer


class SubscribeInfoAuthUserView(generics.RetrieveAPIView):
    """Subscribe info auth user view."""

    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = serializers.SubscribeSerializer
    lookup_field = None

    def get_object(self):
        user = self.request.user

        if user.is_authenticated:
            subscriber = models.Subscriber.objects.filter(user=user).first()

            if subscriber is None:
                subscriber = models.Subscriber.objects.make_subscriber(
                    email=user.email, user=user, ip_address=get_user_ip(self.request),
                    country_code=self.request.country_code, locale=self.request.locale
                )

        else:
            return get_object_or_404(models.Subscriber, user=user)

        return subscriber


class UnsubscribeView(generics.UpdateAPIView):
    """Unsubscribe view."""

    lookup_field = 'update_code'
    lookup_url_kwarg = 'code'
    permission_classes = (permissions.AllowAny,)
    queryset = models.Subscriber.objects.all()
    serializer_class = serializers.SubscribeSerializer

    def patch(self, request, *args, **kw):
        obj = self.get_object()
        obj.unsubscribe(request.query_params)
        serializer = self.get_serializer(instance=obj)
        return Response(data=serializer.data)


class UnsubscribeAuthUserView(generics.GenericAPIView):
    """Unsubscribe auth user view."""

    permission_classes = (permissions.IsAuthenticated,)
    queryset = models.Subscriber.objects.all()
    serializer_class = serializers.SubscribeSerializer

    def patch(self, request, *args, **kw):
        user = request.user
        obj = get_object_or_404(models.Subscriber, user=user)
        obj.unsubscribe(request.query_params)
        serializer = self.get_serializer(instance=obj)
        return Response(data=serializer.data)


class SubscriptionTypesView(generics.ListAPIView):
    pagination_class = None
    permission_classes = (permissions.AllowAny,)
    queryset = models.SubscriptionType.objects.all()
    serializer_class = serializers.SubscriptionTypeSerializer
