from django.contrib import admin

from .forms import DocumentInlineForm, TaskAdminForm, TaskListAdminForm
from .models import Document, Task, TaskList


class DocumentInline(admin.TabularInline):
    model = Document
    form = DocumentInlineForm
    extra = 0
    verbose_name = "Dokument"
    verbose_name_plural = "Do­ku­men­ten­samm­lung"


class TaskAdmin(admin.ModelAdmin):
    form = TaskAdminForm
    model = Task
    inlines = (DocumentInline,)

    readonly_fields = ("slug",)
    fieldsets = (
        (
            None,
            {
                "fields": (
                    "title",
                    "shortterm",
                    "slug",
                    "task_list",
                    "assigned_to",
                    "due_date",
                    "completed",
                    "completed_date",
                    "note",
                    "priority",
                )
            },
        ),
    )

    list_display = [
        "title",
        "task_list",
        "assigned_to",
        "due_date",
        "completed",
        "priority",
    ]
    list_filter = ("task_list",)
    search_fields = ("title",)

    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.created_by = request.user
        super().save_model(request, obj, form, change)


class TaskListAdmin(admin.ModelAdmin):
    form = TaskListAdminForm
    model = TaskList

    readonly_fields = ("slug",)
    fieldsets = (
        (
            None,
            {
                "fields": (
                    "name",
                    "shortterm",
                    "slug",
                    "users",
                )
            },
        ),
    )

    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,
        )


admin.site.register(TaskList, TaskListAdmin)
admin.site.register(Task, TaskAdmin)
