from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.views import generic

from .models import Question, Answer, Questionnaire, Skin_score

class IndexView(generic.ListView):
    template_name = 'questions/index.html'
    context_object_name = 'latest_questionnaire_list' #ListView, the automatically generated context variable is question_list. To override this we provide the context_object_name attribute, specifying that we want to use latest_question_list instead

    def get_queryset(self):
        """Return the last five published questions."""
        return Questionnaire.objects.order_by('-pub_date')[:5]

class DetailView(generic.ListView):
    template_name = 'questions/detail.html'
    context_object_name = 'questions_list' #ListView, the automatically generated context variable is question_list. To override this we provide the context_object_name attribute, specifying that we want to use latest_question_list instead

    def get_queryset(self):
        """Return the last five published questions."""
        return Question.objects.order_by('-pub_date')[:5]

#class DetailView(generic.DetailView):
#    model= Question
#    template_name = 'questions/detail.html' #The template_name attribute is used to tell Django to use a specific template name instead of the autogenerated default template name

class ResultsView(generic.DetailView):
    model= Question
    template_name = 'questions/results.html'

def QuestionnaireView(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Answer.DoesNotExist):
        # Redisplay the question voting form.
        return render(request, 'questions/detail.html', {
            'question': question,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('question_app:results', args=(question.id,)))




# def QuestionnaireView(request, question_id):
#     question = get_object_or_404(Question, pk=question_id)
#     try:
#         selected_answer = question.answer_set.get(pk=request.POST['answer'])
#         current_score = Skin_score.objects.get(pk=1)
#
#     except (KeyError, Answer.DoesNotExist):
#         # Redisplay the question voting form.
#         return render(request, 'questions/detail.html', {
#             'question': question,
#             'error_message':"Please select an answer.",
#         })
#     else:
#
#         current_score.score_1 += selected_answer.score_1
#         current_score.score_2 += selected_answer.score_2
#         current_score.score_3 += selected_answer.score_3
#         current_score.score_4 += selected_answer.score_4
#         current_score.score_5 += selected_answer.score_5
#         #selected_answer.score_1 += 1#
#         current_score.save()

        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        # return HttpResponseRedirect(reverse('question_app:results', args= (question.id,)))
