from django.contrib.contenttypes.models import ContentType
from django.utils.translation import gettext_lazy as _
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import generics, permissions, status
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response

from main import serializers
from main import tasks
from main.filters import AwardFilter
from main.models import Award, Footer, PageType, Panel, SiteFeature, Feature
from main.views import SiteSettingsView, SiteListView


class AwardLstView(generics.ListCreateAPIView):
    """Award list create view."""
    queryset = Award.objects.all()
    serializer_class = serializers.BackAwardSerializer
    permission_classes = (permissions.IsAdminUser,)
    filterset_class = AwardFilter


class AwardRUDView(generics.RetrieveUpdateDestroyAPIView):
    """Award RUD view."""
    queryset = Award.objects.all()
    serializer_class = serializers.BackAwardSerializer
    permission_classes = (permissions.IsAdminUser,)
    lookup_field = 'id'


class ContentTypeView(generics.ListAPIView):
    """ContentType list view"""
    queryset = ContentType.objects.all()
    serializer_class = serializers.ContentTypeBackSerializer
    permission_classes = (permissions.IsAdminUser,)
    filter_backends = (DjangoFilterBackend, )
    ordering_fields = '__all__'
    lookup_field = 'id'
    filterset_fields = (
        'id',
        'model',
        'app_label',
    )


class FeatureBackView(generics.ListCreateAPIView):
    """Feature list or create View."""
    serializer_class = serializers.FeatureSerializer
    queryset = Feature.objects.all()


class SiteFeatureBackView(generics.ListCreateAPIView):
    """Feature list or create View."""
    serializer_class = serializers.SiteFeatureSerializer
    queryset = SiteFeature.objects.all()
    pagination_class = None
    permission_classes = [permissions.IsAdminUser]


class FeatureRUDBackView(generics.RetrieveUpdateDestroyAPIView):
    """Feature RUD View."""
    serializer_class = serializers.FeatureSerializer
    queryset = SiteFeature.objects.all()
    permission_classes = [permissions.IsAdminUser]


class SiteFeatureRUDBackView(generics.RetrieveUpdateDestroyAPIView):
    """Feature RUD View."""
    serializer_class = serializers.SiteFeatureSerializer
    queryset = SiteFeature.objects.all()
    permission_classes = [permissions.IsAdminUser]


class SiteSettingsBackOfficeView(SiteSettingsView):
    """Site settings View."""
    serializer_class = serializers.SiteSerializer


class SiteListBackOfficeView(SiteListView):
    """Site settings View."""
    serializer_class = serializers.SiteSerializer


class FooterBackView(generics.ListCreateAPIView):
    """Footer back list/create view."""
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
    serializer_class = serializers.FooterBackSerializer
    queryset = Footer.objects.all()


class FooterRUDBackView(generics.RetrieveUpdateDestroyAPIView):
    """Footer back RUD view."""
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
    serializer_class = serializers.FooterBackSerializer
    queryset = Footer.objects.all()


class PageTypeListCreateView(generics.ListCreateAPIView):
    """PageType back office view."""
    permission_classes = (permissions.IsAuthenticatedOrReadOnly, )
    pagination_class = None
    serializer_class = serializers.PageTypeBaseSerializer
    queryset = PageType.objects.all()


class PanelsListCreateView(generics.ListCreateAPIView):
    """Custom panels view."""
    permission_classes = (
        permissions.IsAdminUser,
    )
    serializer_class = serializers.PanelSerializer
    queryset = Panel.objects.all()


class PanelsRUDView(generics.RetrieveUpdateDestroyAPIView):
    """Custom panels view."""
    permission_classes = (
        permissions.IsAdminUser,
    )
    serializer_class = serializers.PanelSerializer
    queryset = Panel.objects.all()


class PanelsExecuteView(generics.ListAPIView):
    """Custom panels view."""
    permission_classes = (
        permissions.IsAdminUser,
    )
    queryset = Panel.objects.all()

    def list(self, request, *args, **kwargs):
        panel = get_object_or_404(Panel, id=self.kwargs['pk'])
        return Response(panel.execute_query(request))


class PanelsExportCSVView(PanelsExecuteView):
    """Export panels via csv view."""
    permission_classes = (permissions.IsAdminUser,)
    queryset = Panel.objects.all()

    def list(self, request, *args, **kwargs):
        panel = get_object_or_404(Panel, id=self.kwargs['pk'])
        # make task for celery
        tasks.send_export_to_email.delay(
            panel_id=panel.id, user_id=request.user.id)
        return Response(
            {"success": _('The file will be sent to your email.')},
            status=status.HTTP_200_OK
        )


class PanelsExecuteXLSView(PanelsExecuteView):
    """Export panels via xlsx view."""
    permission_classes = (permissions.IsAdminUser,)
    queryset = Panel.objects.all()

    def list(self, request, *args, **kwargs):
        panel = get_object_or_404(Panel, id=self.kwargs['pk'])
        # make task for celery
        tasks.send_export_to_email.delay(
            panel_id=panel.id, user_id=request.user.id, file_type='xls')
        return Response(
            {"success": _('The file will be sent to your email.')},
            status=status.HTTP_200_OK
        )
