from django.shortcuts import render, redirect
from django.urls import reverse_lazy

from django.http import (
    HttpResponse as res,
)

from .forms import BlogForm, CommentForm
from .models import Blog, Comment

def index(req):
    template = "blog_list.html"
    context = {
        "blogs": Blog.objects.all()
    }
    return render(req, template, context)

def login(req):
    if req.user.is_authenticated:
        return redirect(reverse_lazy("blog:index"))
    else:
        return redirect(reverse_lazy("login"))

def blog_create(req):
    if not req.user.is_authenticated:
        return redirect(reverse_lazy("blog:index"))
    
    template = "blog_form.html"
    
    blog = Blog()
    blog.author = req.user
    blog_form = BlogForm(instance=blog)
    
    if req.method == "POST":
        form = BlogForm(req.POST, instance=blog)
        
        if form.is_valid():
            form.save()
            return redirect(reverse_lazy("blog:index"))
    
    context = {
        "page_title": "Create New Blog",
        "blog_form": blog_form
    }
    
    return render(req, template, context)

def blog_delete(req, blog_id):
    if not req.user.is_authenticated:
        return redirect(reverse_lazy("blog:index"))
        
    blog = Blog.objects.get(id=blog_id)
    blog.delete()
    
    return redirect(reverse_lazy("blog:index"))
        
    return res("Delete blog")

def blog_view(req, blog_id):
    template = "blog_view.html"
    
    blog = Blog.objects.get(id=blog_id)
    comments = Comment.objects.all()
    
    if req.user.is_authenticated:
        comment = Comment()
        comment.author = req.user
        comment.blog = blog
    
    context = {
        "blog": blog,
        "comments": comments.filter(blog=blog),
        "comment": CommentForm()
    }
    return render(req, template, context)

def blog_update(req, blog_id):
    if not req.user.is_authenticated:
        return redirect(reverse_lazy("blog:index"))
    
    template = "blog_form.html"
    blog = Blog.objects.get(id=blog_id)
    
    if req.method == "POST":
        form = BlogForm(req.POST, instance=blog)
        
        if form.is_valid():
            form.save()
            
            return redirect(
                reverse_lazy("blog:view_blog", args=[blog.id]))

    context = {
        "page_title": "Edit Blog",
        "blog_form": BlogForm(instance=blog)
    }
        
    return render(req, template, context)

def comment_add(req, blog_id):
    if not req.user.is_authenticated:
        return redirect(reverse_lazy("blog:index"))
        
    template = "comment_form.html"
    
    comment = Comment()
    comment.author = req.user
    comment.blog = Blog.objects.get(id=blog_id)
    
    if req.method == "POST":
        form = CommentForm(req.POST, instance=comment)
        
        if form.is_valid():
            form.save()
            return redirect(reverse_lazy("blog:view_blog", args=[blog_id]))
            
        else:
            res(form)
    
    context = {
        "page_title": "Add New Comment",
        "comment_form": CommentForm(instance=comment)
    }
    
    return render(req, template, context)

def comment_delete(req, comment_id):
    if not req.user.is_authenticated:
        return redirect(reverse_lazy("blog:index"))
        
    comment = Comment.objects.get(id=comment_id)
    comment.delete()
    
    return redirect(reverse_lazy("blog:view_comments"))


def comment_update(req, comment_id):
    if not req.user.is_authenticated:
        return redirect(reverse_lazy("blog:index"))
        
    template = "comment_form.html"
    comment = Comment.objects.get(id=comment_id)
    
    if req.method == "POST":
        form = CommentForm(req.POST, instance=comment)
        
        if form.is_valid():
            form.save()
            return redirect(reverse_lazy("blog:view_comments"))
    
    context = {
        "page_title": "Edit Comment",
        "comment_form": CommentForm(instance=comment)
    }
        
    return render(req, template, context)

def comments_view(req):
    if not req.user.is_authenticated:
        return redirect(reverse_lazy("blog:index"))
        
    template = "comments_list.html"
    
    comments = Comment.objects.all()
    context = {
        "comments": comments.filter(author=req.user)
    }
    
    return render(req, template, context)
