from django.contrib import admin
from django.db.models import F

from .models import TopicGroup, Topic, Attachment, Etherpad, FileUpload
from .forms import (
    TopicGroupAdminForm,
    TopicAdminForm,
    AttachmentAdminForm,
    EtherpadAdminForm,
    FileUploadAdminForm,
    TopicInlineForm,
    AttachmentInlineForm,
    EtherpadInlineForm,
    FileUploadInlineForm,
)


class TopicInline(admin.TabularInline):
    model = Topic
    form = TopicInlineForm
    extra = 0
    verbose_name = "Thema"
    verbose_name_plural = "Themen"
    show_change_link = True
    readonly_fields = ("slug",)


class AttachmentInline(admin.TabularInline):
    model = Attachment
    form = AttachmentInlineForm
    extra = 0
    verbose_name = "Anhang Ordner"
    verbose_name_plural = "Anhang Ordner"
    readonly_fields = ("slug",)


class EtherpadInline(admin.TabularInline):
    model = Etherpad
    form = EtherpadInlineForm
    extra = 0
    verbose_name = "Etherpad"
    verbose_name_plural = "Etherpads"


class FileUploadInline(admin.TabularInline):
    model = FileUpload
    form = FileUploadInlineForm
    extra = 0
    verbose_name = "Datei"
    verbose_name_plural = "Dateien"


class TopicGroupAdmin(admin.ModelAdmin):
    form = TopicGroupAdminForm
    model = TopicGroup
    search_fields = ("title",)

    readonly_fields = ("slug",)
    fieldsets = (
        (
            None,
            {
                "fields": (
                    "title",
                    "shortterm",
                    "slug",
                    "order",
                    "short_description",
                )
            },
        ),
    )

    inlines = (TopicInline,)

    list_display = ["title", "order"]
    ordering = [F("order").asc(nulls_last=True)]

    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 TopicAdmin(admin.ModelAdmin):
    form = TopicAdminForm
    model = Topic
    search_fields = ("title",)

    readonly_fields = ("slug",)
    fieldsets = (
        (
            None,
            {
                "fields": (
                    "title",
                    "shortterm",
                    "slug",
                    "topic_group",
                    "task_list",
                    "archive",
                    "description",
                )
            },
        ),
    )
    inlines = (AttachmentInline,)

    list_filter = ["topic_group", "archive"]
    list_display = ["title", "topic_group", "archive"]
    ordering = ["archive"]

    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 AttachmentAdmin(admin.ModelAdmin):
    form = AttachmentAdminForm
    model = Attachment

    readonly_fields = ("slug",)
    fieldsets = (
        (
            None,
            {
                "fields": (
                    "title",
                    "shortterm",
                    "slug",
                    "topic",
                    "description",
                )
            },
        ),
    )

    inlines = (
        EtherpadInline,
        FileUploadInline,
    )

    list_display = [
        "title",
        "topic",
    ]

    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 EtherpadAdmin(admin.ModelAdmin):
    form = EtherpadAdminForm
    model = Etherpad

    list_filter = [
        "attachment",
    ]
    list_display = [
        "title",
        "date",
        "attachment",
    ]
    ordering = ["-date"]

    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 FileUploadAdmin(admin.ModelAdmin):
    form = FileUploadAdminForm
    model = FileUpload

    list_filter = [
        "attachment",
    ]
    list_display = [
        "title",
        "attachment",
    ]

    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)


admin.site.register(TopicGroup, TopicGroupAdmin)
admin.site.register(Topic, TopicAdmin)
admin.site.register(Attachment, AttachmentAdmin)
admin.site.register(Etherpad, EtherpadAdmin)
admin.site.register(FileUpload, FileUploadAdmin)
