# encoding: utf-8
from django import forms
from django.contrib import admin, messages
from django.utils.html import format_html

from endorsements.models import Category, Tag
from wikipedia.models import BulkImport, ImportedEndorsement, \
                             ImportedEndorser, ImportedNewspaper, \
                             ImportedResult, ImportedRepresentative, \
                             ElectoralVotes


@admin.register(BulkImport)
class BulkImportAdmin(admin.ModelAdmin):
    list_display = ('slug', 'created_at')
    list_filter = ('slug',)


class ConfirmedEndorserFilter(admin.SimpleListFilter):
    title = 'Confirmed endorser'
    parameter_name = 'is_confirmed'

    def lookups(self, request, model_admin):
        return (
            ('yes', 'Yes'),
            ('no',  'No'),
        )

    def queryset(self, request, queryset):
        if self.value() == 'yes':
            return queryset.filter(confirmed_endorser__isnull=False)
        if self.value() == 'no':
            return queryset.filter(confirmed_endorser__isnull=True)


class NeedsFilter(admin.SimpleListFilter):
    title = 'needs'
    parameter_name = 'needs'

    def lookups(self, request, model_admin):
        return (
            ('tags',  'Tags'),
            ('org_type',  'Org type'),
            ('gender',  'Gender'),
            ('race',  'Race'),
            ('occupation',  'Occupation'),
            ('location',  'Location'),
            ('party',  'Party'),
        )

    def queryset(self, request, queryset):
        value = self.value()
        queryset = queryset.filter(confirmed_endorser__isnull=False)
        if value == 'tags':
            return queryset.filter(confirmed_endorser__tags=None)
        elif value == 'org_type':
            return queryset.filter(
                confirmed_endorser__is_personal=False
            ).exclude(
                confirmed_endorser__tags__category=7
            )
        elif value == 'gender':
            return queryset.filter(
                confirmed_endorser__is_personal=True
            ).exclude(
                confirmed_endorser__tags__category=1
            )
        elif value == 'race':
            return queryset.filter(
                confirmed_endorser__is_personal=True
            ).exclude(
                confirmed_endorser__tags__category=4
            )
        elif value == 'occupation':