# Create your views here.
from django.db import IntegrityError
from django.db.models import Count
from django.http import Http404, HttpResponseRedirect
from django.urls import reverse
from django.views.generic import TemplateView
from django_filters.views import FilterView
from django_tables2 import SingleTableMixin

from applications.models import APP_PENDING
from organizers import models
from organizers.tables import ApplicationsListTable, ApplicationFilter
from user.mixins import IsOrganizerMixin
from user.models import User


def add_vote(application, user, tech_rat, pers_rat):
    v = models.Vote()
    v.user = user
    v.application = application
    v.tech = tech_rat
    v.personal = pers_rat
    v.save()
    return v


def add_comment(application, user, text):
    comment = models.ApplicationComment()
    comment.author = user
    comment.application = application
    comment.text = text
    comment.save()
    return comment


class RankingView(IsOrganizerMixin, TemplateView):
    template_name = 'ranking.html'

    def get_context_data(self, **kwargs):
        context = super(RankingView, self).get_context_data(**kwargs)
        context['ranking'] = User.objects.annotate(
            vote_count=Count('vote__calculated_vote')) \
            .order_by('-vote_count').exclude(vote_count=0).values('vote_count',
                                                                  'email')
        return context


class ApplicationsListView(IsOrganizerMixin, SingleTableMixin, FilterView):
    template_name = 'applications_list.html'
    table_class = ApplicationsListTable
    filterset_class = ApplicationFilter

    def get_queryset(self):
        return models.Application.annotate_vote(models.Application.objects.all())


        # def get_context_data(self, **kwargs):
        #     context = super(ApplicationsListView, self).get_context_data(**kwargs)
        #     apps = models.Application.annotate_vote(models.Application.objects.all())
        #     table = ApplicationsListTable(apps)
        #     RequestConfig(self.request).configure(table)
        #
        #     context.update({
        #         'app_list': table,
        #     })
        #     return context


class ApplicationDetailView(IsOrganizerMixin, TemplateView):
    template_name = 'application_detail.html'

    def get_context_data(self, **kwargs):
        context = super(ApplicationDetailView, self).get_context_data(**kwargs)
        application = self.get_application(kwargs)
        context['app'] = application
        context['vote'] = self.can_vote()
        context['comments'] = models.ApplicationComment.objects.filter(application=application)
        return context

    def can_vote(self):
        return False

    def get_application(self, kwargs):
        application_id = kwargs.get('id', None)
        if not application_id:
            raise Http404
        application = models.Application.objects.filter(uuid=application_id).first()
        if not application:
            raise Http404

        return application

    def post(self, request, *args, **kwargs):
        id_ = request.POST.get('app_id')
        comment_text = request.POST.get('comment_text', None)
        application = models.Application.objects.get(id=id_)

        add_comment(application, request.user, comment_text)
        return HttpResponseRedirect(reverse('app_detail', kwargs={'id': id_}))


class VoteApplicationView(ApplicationDetailView):
    def get_application(self, kwargs):
        """
        Django model to the rescue. This is transformed to an SQL sentence
        that does exactly what we need
        :return: pending aplication that has not been voted by the current
        user and that has less votes and its older
        """
        return models.Application.objects \
            .exclude(vote__user_id=self.request.user.id) \
            .filter(status=APP_PENDING) \
            .annotate(count=Count('vote__calculated_vote')) \
            .order_by('count', 'submission_date') \
            .first()

    def post(self, request, *args, **kwargs):
        tech_vote = request.POST.get('tech_rat', None)
        pers_vote = request.POST.get('pers_rat', None)
        comment_text = request.POST.get('comment_text', None)
        application = models.Application.objects.get(user_id=request.POST.get('app_id'))
        try:
            if request.POST.get('skip'):
                add_vote(application, request.user, None, None)
            elif request.POST.get('add_comment'):
                add_comment(application, request.user, comment_text)
            else:
                add_vote(application, request.user, tech_vote, pers_vote)
        # If application has already been voted -> Skip and bring next
        # application
        except IntegrityError:
            pass
        return HttpResponseRedirect(reverse('vote'))

    def can_vote(self):
        return True

@@ -1748,464 +1748,8 @@
 )%0A%0A%0A
-        # def get_context_data(self, **kwargs):%0A        #     context = super(ApplicationsListView, self).get_context_data(**kwargs)%0A        #     apps = models.Application.annotate_vote(models.Application.objects.all())%0A        #     table = ApplicationsListTable(apps)%0A        #     RequestConfig(self.request).configure(table)%0A        #%0A        #     context.update(%7B%0A        #         'app_list': table,%0A        #     %7D)%0A        #     return context%0A%0A%0A
 clas

remove non-used code