from django.views.generic import DetailView, ListView
from django.shortcuts import get_object_or_404
from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.contrib import messages

from ummeli.base.models import PROVINCE_CHOICES
from ummeli.opportunities.models import *
from ummeli.providers.forms import UploadTaskForm
from ummeli.vlive.utils import get_lat_lon
from django.contrib.gis.geos import Point


class OpportunityDetailView(DetailView):
    def get_object(self):
        return get_object_or_404(self.model, slug=self.kwargs['slug'])


class MicroTaskDetailView(OpportunityDetailView):
    def get_context_data(self, **kwargs):
        context = super(MicroTaskDetailView, self).get_context_data(**kwargs)
        context['city'] = self.request.session['location']['city']
        return context


class CampaignDetailView(OpportunityDetailView):
    def get_context_data(self, **kwargs):
        context = super(CampaignDetailView, self).get_context_data(**kwargs)
        context['has_qualified'] = self.get_object()\
                                        .has_qualified(self.request.user)
        return context


class OpportunityListView(ListView):
    paginate_by = 10

    def get_queryset(self):
        province = int(RequestContext(self.request)['province_id'])
        province_qs = self.model.objects.filter(state='published')

        if province > 0:
            province_qs = province_qs.filter(province__province__in=[province, 0])

        return province_qs.order_by('-created')


class MicroTaskListView(ListView):
    paginate_by = 10

    def get_queryset(self):
        campaign = get_object_or_404(Campaign, slug=self.kwargs['campaign'])

        if not campaign.has_qualified(self.request.user):
            return MicroTask.objects.none()

        position = self.request.session['location']['position']

        if not isinstance(position, Point):
            position = self.request.session['location']['city'].coordinates
        tasks = MicroTask.permitted.filter(campaign__pk=campaign.pk)\
                                .distance(position).order_by('distance')
        return [task for task in tasks if task.available()]

    def get_context_data(self, **kwargs):
        context = super(MicroTaskListView, self).get_context_data(**kwargs)
        campaign = get_object_or_404(Campaign, slug=self.kwargs['campaign'])
        context['campaign'] = campaign
        context['city'] = self.request.session['location']['city']
        print context
        return context


class MyMicroTaskListView(MicroTaskListView):
    def get_queryset(self):
        campaign = get_object_or_404(Campaign, slug=self.kwargs['campaign'])

        if not campaign.has_qualified(self.request.user):
            return MicroTask.objects.none()

        return campaign.tasks.filter(taskcheckout__user=self.request.user,
                                taskcheckout__state=0)\
                            .order_by('-taskcheckout__date')


def opportunities(request):
    context = {
        'campaigns': Campaign.permitted.exists(),
        'bursaries': Bursary.permitted.exists(),
        'internships': Internship.permitted.exists(),
        'volunteering': Volunteer.permitted.exists(),
        'training': Training.permitted.exists(),
        'competitions': Competition.permitted.exists(),
        'events': Event.permitted.exists(),
    }
    return render(request, 'opportunities/opportunities.html', context)


def change_province(request, province=None):
    next = request.GET.get('next', reverse('opportunities'))

    if province and int(province) in range(0, 10):
        request.session['province'] = int(province)
        print request.session['province']
        return redirect(next)

    return render(request, 'opportunities/change_province.html',
                {'provinces': PROVINCE_CHOICES,
                'next': next})


@login_required
def campaign_qualify(request, slug):
    campaign = get_object_or_404(Campaign, slug=slug)
    context = {
        'object': campaign,
    }

    if request.method == 'POST':
        form = UploadTaskForm(request.POST, request.FILES)
        if form.is_valid():
            file = form.cleaned_data['file']
            lat, lon = get_lat_lon(file)
            if lat and lon:
                campaign.qualifiers.add(request.user)
                return redirect(reverse('campaign_detail', args=[slug, ]))
            else:
                context['error'] = True
    else:
        form = UploadTaskForm()

    return render(request, 'opportunities/campaign_qualify.html', context)


@login_required
def checkout(request, slug):
    task = get_object_or_404(MicroTask, slug=slug)
    if task.checkout(request.user):
        msg = 'You have booked this task. You have %shrs to finish the task.'
        messages.add_message(request, messages.SUCCESS, msg % task.hours_per_task)
        return redirect(reverse('micro_task_upload', args=[slug, ]))
    msg = 'That task is no longer available for you.'
    messages.add_message(request, messages.ERROR, msg)
    return redirect(reverse('micro_task_upload', args=[slug, ]))


@login_required
def task_upload(request, slug):
    task = get_object_or_404(MicroTask, slug=slug)
    if not task.taskcheckout_set.filter(user=request.user, state=0).exists():
        messages.add_message(request, messages.ERROR, 'That task is no longer available.')
        return redirect(reverse('campaigns'))

    return render(request, 'opportunities/microtasks/microtask_upload.html',
            {'object': task})

@@ -2615,30 +2615,8 @@
 y'%5D%0A
-        print context%0A
     
@@ -3779,50 +3779,8 @@
 ce)%0A
-        print request.session%5B'province'%5D%0A
     

remove stray prints