"""
Define views for Polls application.
"""

from django.contrib import messages
from django.shortcuts import redirect, render, reverse
from django.views.generic import TemplateView
from django.forms import formset_factory

from .models import Poll, PollAnswer, PollOption
from .forms import PollForm, PollOptionForm, VoteForm

__author__ = "pesusieni999"
__copyright__ = "Copyright 2017, MtG website Project"
__credits__ = ["pesusieni999"]
__license__ = "MIT"
__version__ = "0.0.1"
__maintainer__ = "pesusieni999"
__email__ = "pesusieni999@gmail.com"
__status__ = "Development"


class Polls(TemplateView):
    def get(self, request, *args, **kwargs):
        polls = Poll.objects.all()
        poll_form = PollForm()
        return render(
            request,
            'polls.html',
            context={
                'polls': polls,
                'poll_form': poll_form
            }
        )

    def post(self, request, *args, **kwargs):
        """
        View handler for creating new polls.
        :return: Success -> Redirect to poll, failure -> redirect to polls.
        """
        poll_form = PollForm(data=request.POST)
        if not poll_form.is_valid():
            polls = Poll.objects.all()
            return render(
                request,
                'polls.html',
                context={
                    'polls': polls,
                    'poll_form': poll_form
                }
            )
        poll = poll_form.save(commit=False)
        poll.owner = request.user
        poll.save()
        messages.success(request, 'New poll created.')
        return redirect(reverse('poll', kwargs={'poll_id': poll.id}), request)


class PollView(TemplateView):
    def get(self, request, *args, poll_id=0, **kwargs):
        """
        View for showing single poll information.
        :return: Render poll view (if such poll exists).
        """
        try:
            poll = Poll.objects.get(id=poll_id)
        except Poll.DoesNotExist:
            messages.error(request, "No such poll found.")
            return redirect(reverse('polls'), request)
        poll_form = PollForm(instance=poll)
        poll_options = PollOption.objects.filter(poll=poll.id)
        has_voted = PollAnswer.has_user_voted(request.user, poll)
        return render(
            request,
            'poll.html',
            context={
                'poll': poll,
                'poll_form': poll_form,
                'poll_options': poll_options,
                'has_voted': has_voted
            }
        )

    def post(self, request, poll_id=0, *args, **kwargs):
        """
        View for updating single poll.
        """
        try:
            poll = Poll.objects.get(id=poll_id, owner=request.user)
        except Poll.DoesNotExist:
            messages.error(request, 'No such poll found.')
            return redirect(reverse('polls'), request)
        poll_form = PollForm(data=request.POST, instance=poll)
        if not poll_form.is_valid():
            has_voted = PollAnswer.has_user_voted(user, poll)
            return render(
                request,
                'poll.html',
                context={
                    'poll': poll,
                    'poll_form': poll_form,
                    'has_voted': has_voted
                }
            )
        poll = poll_form.save(commit=False)
        poll.owner = request.user
        poll.save()
        messages.success(request, 'Poll information updated.')
        return redirect(reverse('poll', kwargs={'poll_id': poll.id}), request)


class DeletePoll(TemplateView):
    """
    View for deleting polls.
    """
    def post(self, request, *args, poll_id=0, **kwargs):
        try:
            poll = Poll.objects.get(id=poll_id, owner=request.user)