# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import Group
from coin.members.models import Member, CryptoKey, LdapUser, MembershipFee
from coin.members.membershipfee_filter import MembershipFeeFilter
from coin.members.forms import MemberChangeForm, MemberCreationForm
from coin.utils import delete_selected
import autocomplete_light


class CryptoKeyInline(admin.StackedInline):
    model = CryptoKey
    extra = 0


class MembershipFeeInline(admin.TabularInline):
    model = MembershipFee
    extra = 0
    fields = ('start_date', 'end_date', 'amount')


class MemberAdmin(UserAdmin):
    list_display = ('id', 'status', 'username', 'first_name', 'last_name',
                    'organization_name', 'email', 'end_date_of_membership')
    list_display_links = ('id', 'username', 'first_name', 'last_name')
    list_filter = ('status', MembershipFeeFilter)
    search_fields = ['username', 'first_name', 'last_name', 'email']
    ordering = ('last_name',)
    actions = [delete_selected, 'set_as_member', 'set_as_non_member']

    form = MemberChangeForm
    add_form = MemberCreationForm

    fieldsets = (
        ('Adhérent', {'fields': (
            'status',
            'type',
                ('first_name', 'last_name', 'organization_name'),
                ('entry_date', 'resign_date'))}),
        ('Coordonnées', {'fields': (
            'email',
            ('home_phone_number', 'mobile_phone_number'),
            'address',
            ('postal_code', 'city', 'country'))}),
        ('Authentification', {'fields': (
            ('username', 'password'))}),
        ('Permissions', {'fields': (
            ('is_active', 'is_staff', 'is_superuser'))})
    )

    add_fieldsets = (
        ('Adhérent', {'fields': (
            'status',
            'type',
            ('first_name', 'last_name', 'organization_name'),
            ('entry_date', 'resign_date'))}),
        ('Coordonnées', {'fields': (
            'email',
            ('home_phone_number', 'mobile_phone_number'),
            'address',
            ('postal_code', 'city', 'country'))}),
        ('Authentification', {'fields': (
            ('username', 'password'),)}),
        ('Permissions', {'fields': (
            ('is_active', 'is_staff', 'is_superuser', 'date_joined'))})
    )

    radio_fields = {"type": admin.HORIZONTAL}

    save_on_top = True

    inlines = [CryptoKeyInline, MembershipFeeInline]

    def get_readonly_fields(self, request, obj=None):
        if obj:
            # Remove help_text for readonly field (can't do that in the Form
            # django seems to user help_text from model for readonly fields)
            username_field = [
                f for f in obj._meta.fields if f.name == 'username']
            username_field[0].help_text = ''
            return ['username', ]
        else:
            return []

    def set_as_member(self, request, queryset):
        rows_updated = queryset.update(status='adherent')
        self.message_user(
            request,
            '%d membre(s) définis comme adhérent(s).' % rows_updated)
    set_as_member.short_description = 'Définir comme adhérent'

    def set_as_non_member(self, request, queryset):
        rows_updated = queryset.update(status='non_adherent')
        self.message_user(
            request,
            '%d membre(s) définis comme non adhérent(s).' % rows_updated)
    set_as_non_member.short_description = "Définir comme non adhérent"


class MembershipFeeAdmin(admin.ModelAdmin):
    list_display = ('member', 'end_date', 'amount')
    form = autocomplete_light.modelform_factory(MembershipFee)

admin.site.register(Member, MemberAdmin)
admin.site.register(MembershipFee, MembershipFeeAdmin)
admin.site.unregister(Group)
# admin.site.register(LdapUser, LdapUserAdmin)
