from django.views.generic import ListView
from django.views.generic.detail import DetailView
from django.views.generic.dates import DateDetailView
from django.views.generic.dates import YearArchiveView, MonthArchiveView
from django.contrib.syndication.views import Feed
from django.shortcuts import get_object_or_404
from django.conf import settings
from django.db.models import Q

from utils.views import PaginatedListView

from .models import Entry, HomepageCurrator, Home
from locations.models import LuxCheckIn, Country, Region, Location
from sightings.models import Sighting


class EntryList(PaginatedListView):
    """
    Return a list of Entries in reverse chronological order
    """
    model = Entry

    def get_queryset(self):
        queryset = super(EntryList, self).get_queryset()
        print(queryset)
        return queryset.filter(status__exact=1).order_by('-pub_date').select_related('location').prefetch_related('featured_image')


class EntryCountryList(PaginatedListView):
    """
    Return a list of Entries by Country in reverse chronological order
    """
    template_name = "archives/writing.html"

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(EntryCountryList, self).get_context_data(**kwargs)
        try:
            context['region'] = Country.objects.get(slug__exact=self.kwargs['slug'])
        except:
            context['region'] = Region.objects.get(slug__exact=self.kwargs['slug'])
        return context

    def get_queryset(self):
        try:
            region = Country.objects.get(slug__exact=self.kwargs['slug'])
            qs = Entry.objects.filter(
                status__exact=1,
                location__state__country=region
            ).order_by('-pub_date')
        except:
            region = Region.objects.get(slug__exact=self.kwargs['slug'])
            qs = Entry.objects.filter(
                status__exact=1,
                location__state__country__lux_region=region.id
            ).order_by('-pub_date')
        return qs


class EntryYearArchiveView(YearArchiveView):
    queryset = Entry.objects.filter(status__exact=1).select_related()
    date_field = "pub_date"
    make_object_list = True
    allow_future = True
    template_name = "archives/writing_date.html"


class EntryMonthArchiveView(MonthArchiveView):
    queryset = Entry.objects.filter(status__exact=1).select_related()
    date_field = "pub_date"
    allow_future = True
    template_name = "archives/writing_date.html"


class EntryDetailView(DateDetailView):
    model = Entry
    date_field = 'pub_date'
    slug_field = "slug"

    def get_queryset(self):
        queryset = super(EntryDetailView, self).get_queryset()
        return queryset.select_related('location').prefetch_related('field_notes').prefetch_related('books')

    def get_object(self, queryset=None):
        obj = get_object_or_404(
            self.model,
            slug=self.kwargs['slug'],
            pub_date__month=self.kwargs['month'],
            pub_date__year=self.kwargs['year']
        )
        self.location = obj.location
        return obj

    def get_context_data(self, **kwargs):
        context = super(EntryDetailView, self).get_context_data(**kwargs)
        context['wildlife'] = Sighting.objects.filter(
            Q(location=self.location) |
            Q(location__in=Location.objects.filter(parent=self.location))
        ).select_related().order_by('ap_id', 'ap__apclass__kind').distinct("ap")
        return context


class EntryDetailViewTXT(EntryDetailView):
    template_name = "jrnl/entry_detail.txt"


class HomepageList(ListView):
    """
    Return a main entry and list of Entries in reverse chronological order
    """
    model = Entry

    def get_home(self):
        return Home.objects.filter(pk=1).prefetch_related('featured_image').select_related('featured').select_related('featured__location').get()

    def get_queryset(self):
        queryset = super(HomepageList, self).get_queryset()
        self.home = self.get_home()
        return queryset.filter(status__exact=1).order_by('-pub_date').exclude().select_related('location').select_related('featured_image')[1:9]

    def get_template_names(self):
        return ['%s' % self.home.template_name]

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(HomepageList, self).get_context_data(**kwargs)
        context['homepage'] = self.home
        context['location'] = LuxCheckIn.objects.latest()
        context['IMAGES_URL'] = settings.IMAGES_URL
        return context


class JrnlLatestView(EntryDetailView):
    template_name = "details/entry_latest.html"

    def get_object(self, queryset=None):
        obj = self.model.objects.latest()
        self.location = obj.location
        return obj


class JrnlRSSFeedView(Feed):
    title = "Luxagraf: Topographical Writings"
    link = "/jrnl/"
    description = "Latest postings to luxagraf.net"
    description_template = 'feeds/blog_description.html'

    def items(self):
        return Entry.objects.filter(status__exact=1).order_by('-pub_date')[:10]
    
    def item_pubdate(self, item):
        """
        Takes an item, as returned by items(), and returns the item's
        pubdate.
        """
        return item.pub_date