from django.shortcuts import render
from datetime import datetime, timedelta
import pytz, random

from .models import *
from .serializers import *

from django.db.models import Q
from django.db.models import Count

from rest_framework import generics
from rest_framework.decorators import authentication_classes, permission_classes
from rest_framework.authentication import SessionAuthentication, BasicAuthentication

from rest_framework.permissions import IsAuthenticated
# from durin.auth import TokenAuthentication

# from durin.views import APIAccessTokenView

from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters

from rest_framework.response import Response
from rest_framework_api_key.permissions import HasAPIKey

td = timedelta(hours=7)
odt = datetime.now() - td

# Create your views here.

class CalAPIView(generics.ListAPIView):
    serializer_class = CalSerializer
    queryset = Calendar.objects.all()
    # queryset = Calendar.objects.filter(published=True)
    permission_classes = [HasAPIKey]


class EventsAPIView(generics.ListAPIView):
    serializer_class = EventSerializer
#    queryset = Event.objects.filter(show_date__gte=odt).order_by('show_day')
    queryset = Event.objects.all()
#    filter_backends = [DjangoFilterBackend, filters.SearchFilter]
#    filterset_fields = ['show_title', 'event_type', 'venue__name','show_day', 'calendar__shortcode', 'show_date',]
#    search_fields = ['show_title', 'event_type','show_day', 'venue__name', 'calendar__shortcode',]
    # permission_classes = [HasAPIKey]

    def get_queryset(self):
        calendar = self.request.GET.get('calendar__shortcode')
        show_day = self.request.GET.get('show_date')
        if self.request.GET.get('event_type') != None:
            event_type = self.request.GET.get('event_type')
            queryset = Event.objects.filter(event_type=event_type,calendar__shortcode=calendar,show_date__gte=show_day).order_by('show_date')[:126]
            return queryset
        queryset = Event.objects.filter(calendar__shortcode=calendar,show_date__gte=show_day).order_by('show_date')[:126]
        return queryset


class PromoAPIView(generics.ListAPIView):
    serializer_class = PromoSerializer
    queryset = Promo.objects.filter(published=True)
    filterset_fields = ['organization__name', 'calendar__shortcode',]
    search_fields = ['organization__name', 'calendar__shortcode',]
    # permission_classes = [HasAPIKey]
    
    def get_queryset(self):
        calendar = self.request.GET.get('calendar__shortcode')
        queryset = Promo.objects.filter(published=True, calendar__shortcode=calendar).order_by('?')
        return queryset

class BizAPIView(generics.ListAPIView):
    serializer_class = OrganizationSerializer
    queryset = Organization.objects.all()[:10]
    # filterset_fields = ['organization__name', 'calendar__shortcode',]
    # search_fields = ['organization__name', 'calendar__shortcode',]
    permission_classes = [HasAPIKey]
    
    # def get_queryset(self):
    #     calendar = self.request.GET.get('calendar__shortcode')
    #     queryset = Promo.objects.filter(published=True, calendar__shortcode=calendar).order_by('?')
    #     return queryset

# class EventsTokenAPIView(APIAccessTokenView):
#     serializer_class = EventSerializer
#     authentication_classes = (TokenAuthentication, BasicAuthentication,)
#     permission_classes = (IsAuthenticated,)
#     queryset = Event.objects.filter(show_date__gte=odt).order_by('show_date')
#     filter_backends = [DjangoFilterBackend, filters.SearchFilter]
#     filterset_fields = ['show_title', 'event_type', 'show_date', 'show_day', 'venue__name']
#     search_fields = ['show_title', 'event_type']
