from django.shortcuts import render,get_object_or_404,redirect
from django.http import HttpResponse,Http404
from django.contrib.auth.models import User
from .forms import NewTopicForm,PostForm
from django.db.models import Count
from django.utils import timezone


from .models import Board,Topic,Post

from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.utils.decorators import method_decorator
from django.views.generic import UpdateView
from django.views.generic import ListView

# Create your views here.

def home(request):
	boards = Board.objects.all()
	context = {
		'title' : 'Django Boards',
		'boards' : boards
	}
	return render(request,'home.html',context)


def board_topics(request,pk):
	board = get_object_or_404(Board, pk=pk)
	queryset = board.topics.order_by('-last_updated').annotate(replies=Count('posts') - 1)
	page = request.GET.get('page', 1)
	paginator = Paginator(queryset, 20)
	try:
		topics = paginator.page(page)
	except PageNotAnInteger:
		topics = paginator.page(1)
	except EmptyPage:
		topics = paginator.page(paginator.num_pages)
	context = {
		'board' : board,
		'topics' : topics
	}
	return render(request,'topics.html',context)


@login_required(login_url="/accounts/login")
def new_topic(request,pk):
	board = get_object_or_404(Board, pk=pk)
	#user = User.objects.first()
	if request.method == 'POST':
		form = NewTopicForm(request.POST)
		if form.is_valid():
			topic = form.save(commit=False)
			topic.board = board
			topic.starter = request.user
			topic.save()
			post = Post.objects.create(
                message=form.cleaned_data.get('message'),
                topic=topic,
                created_by=request.user
			)
			return redirect('boards:topic_posts',pk=pk,topic_pk=topic.pk)
	else:
		form = NewTopicForm()

	context = {
			'title' : 'Start a New Topic',
			'board' : board,
			'form': form
		}
	return render(request, 'new_topic.html',context)


@method_decorator(login_required, name='dispatch')
class PostUpdateView(UpdateView):
	model = Post
	fields = ('message', )
	template_name = 'edit_post.html'
	pk_url_kwarg = 'post_pk'
	context_object_name = 'post'

	def get_queryset(self):
		queryset = super().get_queryset()
		return queryset.filter(created_by=self.request.user)

	def form_valid(self,form):
		post = form.save(commit=False)
		post.updated_by = self.request.user
		post.updated_at = timezone.now()
		post.save()
		return redirect('boards:topic_posts', pk=post.topic.board.pk, topic_pk=post.topic.pk)



def topic_posts(request,pk,topic_pk):
	topic = get_object_or_404(Topic, board__pk=pk, pk=topic_pk)
	topic.views += 1
	topic.save()
	context = {
		'title' : topic.subject,
		'topic' : topic,
	}
	return render(request, 'topic_posts.html',context)


class PostListView(ListView):
	model = Post
	context_object_name = 'posts'
	template_name = 'topic_posts.html'
	paginate_by = 2

	def get_context_data(self,**kwargs):
		self.topic.views += 1
		self.topic.save()
		kwargs['topic'] = self.topic
		return super().get_context_data(**kwargs)

	def get_queryset(self):
		self.topic = get_object_or_404(Topic, board__pk=self.kwargs.get('pk'), pk=self.kwargs.get('topic_pk'))
		queryset = self.topic.posts.order_by('created_at')
		return queryset


@login_required(login_url="/accounts/login")
def reply_topic(request,pk,topic_pk):
	topic = get_object_or_404(Topic, board__pk=pk, pk=topic_pk)
	if request.method == 'POST':
		form = PostForm(request.POST)
		if form.is_valid():
			post = form.save(commit=False)
			post.topic = topic
			post.created_by = request.user
			post.save()

			topic.last_updated = timezone.now()
			topic.save()
			return redirect('boards:topic_posts', pk=pk, topic_pk=topic_pk)
	else:
		form = PostForm()
	context = {
		'topic' : topic,
		'form' : form
	}
	return render(request, 'reply_topic.html',context)

@method_decorator(login_required, name='dispatch')
class TopicListView(ListView):
	model = Topic
	context_object_name = 'topics'
	template_name = 'topics.html'
	paginate_by = 20

	def get_context_data(self,**kwargs):
		kwargs['board'] = self.board
		kwargs['title'] = self.board.name
		return super().get_context_data(**kwargs)

	def get_queryset(self):
		self.board = get_object_or_404(Board, pk=self.kwargs.get('pk'))
		queryset = self.board.topics.order_by('-last_updated').annotate(replies=Count('posts') - 1)
		return queryset


