from django.http import HttpResponseRedirect
from django.shortcuts import render,get_object_or_404
from django.contrib.auth.mixins import LoginRequiredMixin,UserPassesTestMixin
from django.contrib.auth.models import User
from django.urls.base import reverse
from django.views.generic import ListView,DetailView,CreateView,UpdateView,DeleteView
from django.http import HttpResponse, request
from .models import Comment, Post 
from django.db.models import Q, query
# Create your views here.





def home(request):
    context={
        'posts':Post.objects.all()
    }
    return render(request,'blog/home.html',context)

class PostListView(ListView):
    model=Post
    template_name='blog/home.html' #<app>/<model>_<viewtype>.html
    context_object_name='posts'
    ordering=['-date_posted']
    paginate_by=5


class UserPostListView(ListView):
    model=Post
    template_name='blog/user_posts.html' #<app>/<model>_<viewtype>.html
    context_object_name='posts'
    
    paginate_by=5

    def get_queryset(self):
        user=get_object_or_404(User,username=self.kwargs.get('username'))
        return Post.objects.filter(author=user).order_by('-date_posted')






class PostDetailView(DetailView):
    model=Post
    def get_context_data(self, **kwargs):
        data = super(PostDetailView,self).get_context_data(**kwargs)
        
        info_about_likes=get_object_or_404(Post,id=self.kwargs['pk'])
        total_likes=info_about_likes.total_likes()
        data['total_likes']= total_likes
        return data



class PostCreateView(LoginRequiredMixin,CreateView):
    model=Post
    fields=['title','content','header_image']


    def form_valid(self,form):
        form.instance.author=self.request.user
        return super().form_valid(form)


class PostUpdateView(LoginRequiredMixin,UserPassesTestMixin,UpdateView):
    model=Post
    fields=['title','content']


    def form_valid(self,form):
        form.instance.author=self.request.user
        return super().form_valid(form)

    def test_func(self):
        post=self.get_object()
        if self.request.user==post.author:
            return True
        return False



class PostDeleteView(LoginRequiredMixin,UserPassesTestMixin,DeleteView):
    model=Post
    success_url= "/"

    def test_func(self):
        post=self.get_object()
        if self.request.user==post.author:
            return True
        return False



def about(request):
    return render(request,'blog/about.html',{'title':'About'})


def collegestuddes(request):
    return render(request,'blog/eastereggs.html',{'title':'eastereggs'})

class CommentCreateView(LoginRequiredMixin,CreateView):
    model=Comment
    
    tamplate_name='comment_new.html'
    fields=('comment',)

    
    def form_valid(self, form):
        form.instance.author = self.request.user
        form.instance.post = Post.objects.get(pk=self.kwargs['pk'])
        return super().form_valid(form)

def LikeView(request,pk):
    post=get_object_or_404(Post,id=pk)
    
    post.likes.add(request.user)
    return HttpResponseRedirect(reverse('post-detail',args=[str(pk)]))

def SearchView(request):
    if request.method=="POST":
        searched=request.POST.get('searched')
        searches=searched.split(" ")
        for search in searches:
            
            search_results=Post.objects.filter(Q(title__icontains=search)).distinct()

        return render(request,'blog/search.html',{'searched':searched,
        'search_results':search_results})
    else:
        return render(request,'blog/search.html',{'title':'Blog-search'})
