from django.contrib import admin
from django.utils.translation import gettext as _

from .forms import JobForm, JobGroupForm, MemberForm
from .models import Job, JobGroup, JobMember, Member


class MemberRoleFilter(admin.SimpleListFilter):
    title = _("Rolle")
    parameter_name = "role"

    def lookups(self, request, model_admin):
        return (
            ("A", _("Aktiv")),
            ("P", _("Pension")),
        )

    def queryset(self, request, queryset):
        if self.value() in Member.MemberRole:
            return queryset.filter(role=self.value())


class JobMemberInline(admin.TabularInline):
    model = JobMember
    extra = 0


class JobOverviewInline(JobMemberInline):
    verbose_name = "Tätigkeit"
    verbose_name_plural = "Tätigkeitsübersicht"


class ActiveMemberInline(JobMemberInline):
    verbose_name = "Mitglied"
    verbose_name_plural = "Aktive Mitglieder Liste"

    def get_queryset(self, request):
        return JobMember.active_member.get_queryset()


class InactiveMemberInline(JobMemberInline):
    verbose_name = "Mitglied"
    verbose_name_plural = "Inaktive Mitglieder Liste"

    def get_queryset(self, request):
        return JobMember.inactive_member.get_queryset()


class JobInline(admin.TabularInline):
    model = Job
    extra = 0


class MemberAdmin(admin.ModelAdmin):
    form = MemberForm
    model = Member
    fieldsets = (
        (
            None,
            {
                "fields": (
                    (
                        "firstname",
                        "surname",
                    ),
                    "nickname",
                    "username",
                    "mailaccount",
                    "role",
                    "description",
                    "image",
                    "birthday",
                    "phone",
                    "address",
                )
            },
        ),
    )
    inlines = (JobOverviewInline,)

    list_display = ["nickname", "firstname", "surname", "mailaccount", "role"]
    ordering = [
        "firstname",
    ]
    search_fields = ["firstname", "surname", "nickname", "mailaccount"]
    list_filter = [MemberRoleFilter]

    def add_view(self, request, form_url="", extra_context=None):
        extra_context = extra_context or {}
        extra_context["help_text"] = "Fette Schriften sind Pflichtfelder."
        return super().add_view(
            request,
            form_url,
            extra_context=extra_context,
        )

    def change_view(self, request, object_id, form_url="", extra_context=None):
        extra_context = extra_context or {}
        extra_context["help_text"] = "Fette Schriften sind Pflichtfelder."
        return super().change_view(
            request,
            object_id,
            form_url,
            extra_context=extra_context,
        )

    def save_model(self, request, obj, form, change):
        obj.author = request.user
        super().save_model(request, obj, form, change)


class JobAdmin(admin.ModelAdmin):
    form = JobForm
    model = Job
    inlines = (ActiveMemberInline, InactiveMemberInline)

    list_display = [
        "name",
        "job_group",
    ]
    ordering = [
        "name",
    ]
    search_fields = [
        "name",
    ]

    def add_view(self, request, form_url="", extra_context=None):
        extra_context = extra_context or {}
        extra_context["help_text"] = "Fette Schriften sind Pflichtfelder."
        return super().add_view(
            request,
            form_url,
            extra_context=extra_context,
        )

    def change_view(self, request, object_id, form_url="", extra_context=None):
        extra_context = extra_context or {}
        extra_context["help_text"] = "Fette Schriften sind Pflichfelder."
        return super().change_view(
            request,
            object_id,
            form_url,
            extra_context=extra_context,
        )

    def save_model(self, request, obj, form, change):
        obj.author = request.user
        super().save_model(request, obj, form, change)


class JobGroupAdmin(admin.ModelAdmin):
    form = JobGroupForm
    model = JobGroup
    inlines = (JobInline,)

    list_display = [
        "name",
        "is_pinned",
    ]
    ordering = [
        "name",
    ]
    search_fields = [
        "name",
    ]

    def add_view(self, request, form_url="", extra_context=None):
        extra_context = extra_context or {}
        extra_context["help_text"] = "Fette Schriften sind Pflichtfelder."
        return super().add_view(
            request,
            form_url,
            extra_context=extra_context,
        )

    def change_view(self, request, object_id, form_url="", extra_context=None):
        extra_context = extra_context or {}
        extra_context["help_text"] = "Fette Schriften sind Pflichfelder."
        return super().change_view(
            request,
            object_id,
            form_url,
            extra_context=extra_context,
        )

    def save_model(self, request, obj, form, change):
        obj.author = request.user
        super().save_model(request, obj, form, change)


admin.site.register(Member, MemberAdmin)
admin.site.register(Job, JobAdmin)
admin.site.register(JobGroup, JobGroupAdmin)
