"""Translation app views."""
from rest_framework import generics, permissions

from translation import models, serializers
from utils.views import JWTGenericViewMixin


# Mixins
class LanguageViewMixin(generics.GenericAPIView):
    """Mixin for Language views"""

    queryset = models.Language.objects.active()
    serializer_class = serializers.LanguageSerializer


# Views
class LanguageListView(LanguageViewMixin, generics.ListAPIView):
    """List view for model Language"""

    pagination_class = None
    permission_classes = (permissions.IsAuthenticatedOrReadOnly, )


class LanguageRetrieveView(LanguageViewMixin, generics.RetrieveAPIView):
    """Retrieve, Update, Destroy view for model Language"""


class SiteInterfaceDictionaryMixin:

    # todo: refactor this
    def get_queryset(self):
        locale = self.request.locale or 'en'
        return models.SiteInterfaceDictionary.objects.annotate_localized_fields(
            locale=locale)


class SiteInterfaceDictionaryView(JWTGenericViewMixin,
                                  SiteInterfaceDictionaryMixin,
                                  generics.ListAPIView):
    """Site interface dictionary LC view."""

    pagination_class = None
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
    serializer_class = serializers.SiteInterfaceDictionarySerializer
    filter_fields = ['page', 'keywords']


class SiteInterfaceDictionaryRetrieveView(JWTGenericViewMixin,
                                          SiteInterfaceDictionaryMixin,
                                          generics.RetrieveAPIView):
    """Site interface dictionary RUD view."""

    permission_classes = (permissions.AllowAny,)
    serializer_class = serializers.SiteInterfaceDictionarySerializer
