from django.views.generic import ListView
from django.views.generic.detail import DetailView
from django.apps import apps
from django.contrib.syndication.views import Feed
from django.urls import reverse
from django.conf import settings

#from paypal.standard.forms import PayPalPaymentsForm
from utils.views import PaginatedListView, LuxDetailView
from ..models import Post, PostType
from taxonomy.models import Category


class SrcListView(PaginatedListView):
    model = Post
    template_name="posts/src_list.html"

    def get_queryset(self):
        queryset = super(SrcListView, self).get_queryset()
        return queryset.filter(post_type=PostType.SRC).filter(status__exact=1).order_by('-pub_date')

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(SrcListView, self).get_context_data(**kwargs)
        #context['topics'] = Topic.objects.all()
        context['breadcrumbs'] = ['src',]
        return context


class SrcDetailView(LuxDetailView):
    model = Post
    slug_field = "slug"
    template_name="posts/src_detail.html"

    def get_context_data(self, **kwargs):
        context = super(SrcDetailView, self).get_context_data(**kwargs)
        related = []
        for obj in self.object.related.all():
            model = apps.get_model(obj.model_name.app_label, obj.model_name.model)
            related.append(model.objects.get(slug=obj.slug, pub_date=obj.pub_date))
        context['related'] = related
        return context

class SrcDetailViewTXT(SrcDetailView):
    template_name = "posts/jrnl_detail.txt"


class TopicListView(ListView):
    template_name = 'posts/topic_list.html'

    def get_queryset(self):
        return Post.objects.filter(topics__slug=self.kwargs['slug'],post_type=PostType.SRC)

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(TopicListView, self).get_context_data(**kwargs)
        context['topic'] = Category.objects.get(slug__exact=self.kwargs['slug'])
        return context


class SrcRSSFeedView(Feed):
    title = "luxagraf:src Code and Technology"
    link = "/src/"
    description = "Latest postings to luxagraf.net/src"
    description_template = 'feeds/blog_description.html'

    def items(self):
        return Post.objects.filter(status__exact=1).filter(post_type=PostType.SRC).order_by('-pub_date')[:10]


"""
class BookListView(ListView):
    template_name = "archives/src_books.html"

    def queryset(self):
        return Book.objects.filter(status__exact=1)


class BookDetailView(DetailView):
    model = Book

    def get_template_names(self):
        book = self.get_object()
        return [book.template_name]

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(BookDetailView, self).get_context_data(**kwargs)
        book = self.get_object()
        if book.price_sale < book.price:
            price = book.price_sale
        else:
            price = book.price
        #paypal_dict = {
        #    "business": settings.PAYPAL_RECEIVER_EMAIL,
        #    "amount": price,
        #    "item_name": book.title,
        #    "invoice": "unique-invoice-id",
        #    "notify_url": "https://luxagraf.net/src/paypal/" + reverse('src:paypal-ipn'),
        #    "return_url": "https://luxagraf.net/src/thank-you",
        #    "cancel_return": "https://luxagraf.net/src/books/",
        #}
        #context['paypal_form'] = PayPalPaymentsForm(initial=paypal_dict)
        return context

"""