from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.core.paginator import Paginator
from django.contrib.auth import authenticate, login

from .models import Question, Answer
from .forms import AskForm, AnswerForm, SignupForm, LoginForm


def test(request, *args, **kwargs):
    return HttpResponse('OK')


def new_questions_list(request):
    try:
        page = int(request.GET.get('page'))
    except ValueError:
        page = 1
    except TypeError:
        page = 1
    questions = Question.objects.new()
    limit = 10
    paginator = Paginator(questions, limit)
    page = paginator.page(page)
    return render(request, 'index.html', {
        'questions': page.object_list,
        'paginator': paginator,
        'page': page,
        'user': request.user,
        'session': request.session,
    })


def popular_questions_list(request):
    try:
        page = int(request.GET.get('page'))
    except ValueError:
        page = 1
    except TypeError:
        page = 1
    questions = Question.objects.popular()
    limit = 10
    paginator = Paginator(questions, limit)
    page = paginator.page(page)
    return render(request, 'index.html', {
        'questions': page.object_list,
        'paginator': paginator,
        'page': page,
        'user': request.user,
        'session': request.session,
    })


def question_details(request, num):
    question = get_object_or_404(Question, id=num)
    answers = Answer.objects.filter(question=question.pk).order_by('-added_at')[0:10]
    if request.method == "POST":
        answer_form = AnswerForm(request.POST)
        if answer_form.is_valid():
            answer_form._user = request.user
            answer = answer_form.save()
            url = answer.question.get_url()
            return HttpResponseRedirect(url)
    else:
        answer_form = AnswerForm(initial={'question': question.pk})
        answer_form._user = request.user

    return render(request, 'question.html', {
        'question': question,
        'answers': answers,
        'user': request.user,
        'form': answer_form,
    })
    

def ask(request):
    if request.method == "POST":
        form = AskForm(request.POST)
        if form.is_valid():
            form._user = request.user
            question = form.save()
            url = question.get_url()
            return HttpResponseRedirect(url)
    else:
        form = AskForm()
        form._user = request.user
    return render(request, 'ask.html', {'form': form, 
                                        'user': request.user,
                                        'session': request.session,
                                        })

def signup(request):
    if request.method == "POST":
        form = SignupForm(request.POST)
        if form.is_valid():
            user = form.save()
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponseRedirect("/")
    form = SignupForm()
    return render(request, 'signup.html', {'form': form,
                                           'user': request.user,
                                           'session': request.session, })

def my_login(request):
    error = ''
    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            user = form.save()
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponseRedirect('/')
    form = LoginForm()
    return render(request, 'login.html', {'error': error,
                                          'form': form,
                                          'user': request.user,
                                          'session': request.session, })
