'''
App Api's
'''
from app.models import AppVersion, City, State, Country

from app.serializers import (CitySerializer,
                             StateSerializer, CountrySerializer)
from django.conf import settings
from django.db.models import Q
from hotelmanage.core.permissions import (PrivateTokenAccessPermission,
                                       PublicTokenAccessPermission)
from rest_framework import generics, status

from rest_framework.response import Response



class CountryListAPIView(generics.ListAPIView):
    '''
    Country List
    '''
    queryset = Country.objects.filter(is_active=True)
    serializer_class = CountrySerializer
    permission_classes = (PublicTokenAccessPermission, )

class StateListAPIView(generics.ListAPIView):
    '''
    State List
    '''
    serializer_class = StateSerializer
    permission_classes = (PublicTokenAccessPermission, )

    def get_queryset(self):
        '''
        Returns the queryset that will be used to retrieve
        the object that this view will display.
        By default, get_queryset() returns the value of the
        queryset attribute if it is set, otherwise it constructs
        a QuerySet by calling the all() method on the model attribute’s default manager.
        '''
        queryset = State.objects.filter(is_active=True)
        if 'country' in self.kwargs:
            return queryset.filter(country__pk=self.kwargs['country'])
        else:
            return queryset

class CityListAPIView(generics.ListAPIView):
    '''
    City List
    '''
    serializer_class = CitySerializer
    permission_classes = (PublicTokenAccessPermission, )

    def get_queryset(self):
        '''
        Returns the queryset that will be used to retrieve
        the object that this view will display.
        By default, get_queryset() returns the value of the
        queryset attribute if it is set, otherwise it constructs
        a QuerySet by calling the all() method on the model attribute’s default manager.
        '''
        queryset = City.objects.filter(
        is_active=True)
        if 'state' in self.kwargs:
            return queryset.filter(state__pk=self.kwargs['state'])
        else:
            return queryset

