from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
from rest_framework.serializers import Serializer
from rest_framework.permissions import AllowAny, IsAuthenticated

from sinergi.sinergi.models import PegawaiAktif
from navigator.banner.models import Slide

from .serializers import PegawaiSerializer, BannerSerializer
from . import serializers


@api_view(['GET'])
@permission_classes([AllowAny])
def getRoutes(request):
    routes = [
        'GET /api',
        'GET /api/pegawai/',
        'GET /api/pegawai/:nip',
        'GET /api/banner/',
        'GET /api/banner/:id',
    ]

    return Response(routes)


@api_view(['GET'])
@permission_classes([IsAuthenticated])
def getPegawais(request):
    paginator = PageNumberPagination()
    paginator.page_size = 10

    pegawai = PegawaiAktif.objects.all()
    result_page = paginator.paginate_queryset(pegawai, request)

    serializer = PegawaiSerializer(result_page, many=True)
    return paginator.get_paginated_response(serializer.data)


@api_view(['GET'])
@permission_classes([IsAuthenticated])
def getPegawai(request, nip):
    pegawai = PegawaiAktif.objects.get(nipbaru=nip)
    serializer = PegawaiSerializer(pegawai, many=False)
    return Response(serializer.data)


@api_view(['GET'])
@permission_classes([AllowAny])
def getBanners(request):
    paginator = PageNumberPagination()
    paginator.page_size = 10

    slide = Slide.objects.all()
    result_page = paginator.paginate_queryset(slide, request)

    serializer = BannerSerializer(result_page, many=True)
    return paginator.get_paginated_response(serializer.data)


@api_view(['GET'])
@permission_classes([IsAuthenticated])
def getBanner(request, id):
    slide = Slide.objects.get(id=id)
    serializer = BannerSerializer(slide, many=False)
    return Response(serializer.data)
