from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse, JsonResponse
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required

from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic.edit import DeleteView


from . import models
from . import forms

# Create your views here.
@login_required
def index(request):
    if request.method =="POST":
        suggestion_form = forms.SuggestionForm(request.POST)
        if suggestion_form.is_valid():
            suggestion_form.save(request)
            suggestion_form = forms.SuggestionForm()
    else:
        suggestion_form = forms.SuggestionForm()
    
    suggestion_objects = models.SuggestionModel.objects.all()
    suggestion_list = []
    for sugg in suggestion_objects:
        comment_objects = models.CommentModel.objects.filter(suggestion=sugg)
        temp_sugg = {}
        temp_sugg["suggestion"] = sugg.suggestion
        temp_sugg["id"] = sugg.id
        temp_sugg["author"] = sugg.author.username
        temp_sugg["comments"] = comment_objects

        #new additions for date/time here
        temp_sugg["date_input"] = sugg.date_input
        temp_sugg["time_input"] = sugg.time_input
        suggestion_list+=[temp_sugg]

    context = {
        "title":"Suggestion",
        "suggestions":suggestion_list,
        "form":suggestion_form,
    }
    return render(request, "index.html", context=context)

def get_suggestions(request):
    suggestion_objects = models.SuggestionModel.objects.all()
    suggestion_list = {}
    suggestion_list["suggestions"]=[]
    for sugg in suggestion_objects:
        comment_objects = models.CommentModel.objects.filter(suggestion=sugg)
        temp_sugg = {}
        temp_sugg["suggestion"]=sugg.suggestion
        temp_sugg["author"]=sugg.author.username
        temp_sugg["id"]=sugg.id
        temp_sugg["date_input"] = sugg.date_input
        temp_sugg["time_input"] = sugg.time_input
        temp_sugg["comments"]=[]
        # for comm in comment_objects:
        #     temp_comm={}
        #     temp_comm["comment"]=comm.comment
        #     temp_comm["id"]=comm.id
        #     temp_comm["author"]=comm.author.username
        #     temp_sugg["comments"]+=[temp_comm]
        suggestion_list["suggestions"]+=[temp_sugg]
    return JsonResponse(suggestion_list)

def register(request):
    if request.method == "POST":
        form_instance = forms.RegistrationForm(request.POST)
        if form_instance.is_valid():
            form_instance.save()
            return redirect("/login/")
    else:
        form_instance = forms.RegistrationForm()
        context = {
            "form":form_instance,
        }
        return render(request, "registration/registration.html", context=context)

def logout_view(request):
    logout(request)
    return redirect("/login/")

def delete(request, id):
    app = get_object_or_404(models.SuggestionModel, id=id)
    app.delete()
    return redirect("/")

from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse, JsonResponse
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required

from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic.edit import DeleteView


from . import models
from . import forms

# Create your views here.
@login_required
def index(request):
    if request.method =="POST":
        suggestion_form = forms.SuggestionForm(request.POST)
        if suggestion_form.is_valid():
            suggestion_form.save(request)
            suggestion_form = forms.SuggestionForm()
    else:
        suggestion_form = forms.SuggestionForm()
    
    suggestion_objects = models.SuggestionModel.objects.all()
    suggestion_list = []
    for sugg in suggestion_objects:
        comment_objects = models.CommentModel.objects.filter(suggestion=sugg)
        temp_sugg = {}
        temp_sugg["suggestion"] = sugg.suggestion
        temp_sugg["id"] = sugg.id
        temp_sugg["author"] = sugg.author.username
        temp_sugg["comments"] = comment_objects

        #new additions for date/time here
        temp_sugg["date_input"] = sugg.date_input
        temp_sugg["time_input"] = sugg.time_input
        suggestion_list+=[temp_sugg]

    context = {
        "title":"Suggestion",
        "suggestions":suggestion_list,
        "form":suggestion_form,
    }
    return render(request, "index.html", context=context)

def get_suggestions(request):
    suggestion_objects = models.SuggestionModel.objects.all()
    suggestion_list = {}
    suggestion_list["suggestions"]=[]
    for sugg in suggestion_objects:
        comment_objects = models.CommentModel.objects.filter(suggestion=sugg)
        temp_sugg = {}
        temp_sugg["suggestion"]=sugg.suggestion
        temp_sugg["author"]=sugg.author.username
        temp_sugg["id"]=sugg.id
        temp_sugg["date_input"] = sugg.date_input
        temp_sugg["time_input"] = sugg.time_input
        temp_sugg["comments"]=[]
        # for comm in comment_objects:
        #     temp_comm={}
        #     temp_comm["comment"]=comm.comment
        #     temp_comm["id"]=comm.id
        #     temp_comm["author"]=comm.author.username
        #     temp_sugg["comments"]+=[temp_comm]
        suggestion_list["suggestions"]+=[temp_sugg]
    return JsonResponse(suggestion_list)

def register(request):
    if request.method == "POST":
        form_instance = forms.RegistrationForm(request.POST)
        if form_instance.is_valid():
            form_instance.save()
            return redirect("/login/")
    else:
        form_instance = forms.RegistrationForm()
        context = {
            "form":form_instance,
        }
        return render(request, "registration/registration.html", context=context)

def logout_view(request):
    logout(request)
    return redirect("/login/")

def delete(request, id):
    app = get_object_or_404(models.SuggestionModel, id=id)
    app.delete()
    return redirect("/")

# Create your views here. #new additions for date/time here # for comm in comment_objects: #     temp_comm={} #     temp_comm["comment"]=comm.comment #     temp_comm["id"]=comm.id #     temp_comm["author"]=comm.author.username #     temp_sugg["comments"]+=[temp_comm]