from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse, Http404
from django.views.generic.base import View
from django.utils import timezone
from datetime import datetime

from .forms import CommentaryForm

from .models import (
	Category,
	Tag,
	Post,
	Commentary,
)


class CherryPickView(View):

    def get_queryset(self):
        return Post.objects.filter(published_date__lte=datetime.now(), published=True)

    def get(self, request, category_slug=None, slug=None):
        category_list = Category.objects.filter(published=True)
        if category_slug is not None:
            posts = self.get_queryset().filter(category__slug=category_slug, category__published=True)
        elif slug is not None:
            posts = self.get_queryset().filter(tags__slug=slug, tags__published=True)
        else:
            posts = self.get_queryset()
        if posts.exists():
            template = posts.first().get_category_template()
        else:
            # template = "blog/post_list.html"
            raise Http404()
        return render(request, template, {"post_list": posts, 'categories':category_list})


class PostDetailView(View):
    """Show specific post"""
    def get(self, request, **kwargs):
        category_list = Category.objects.filter(published=True)
        post = get_object_or_404(Post, slug=kwargs.get("slug"))
        form = CommentaryForm()
        return render(request, post.template, 
            {"categories": category_list, 
            "post": post,
            "form": form}
        )


    def post(self, request, **kwargs):
        form = CommentaryForm(request.POST)
        if form.is_valid():
            form = form.save(commit=False)
            form.post = Post.objects.get(slug=kwargs.get("slug"))
            form.author = request.user
            form.save()
        return redirect(request.path)



# class CreateCommentaryView(View):
#     def post(self, request, pk):
#         form = CommentaryForm(request.POST)
#         if form.is_valid():
#             form            = form.save(commit=False)
#             form.post_id    = pk
#             form.author     = request.user
#             form.save()
#         return HttpResponse(status=201)