from django.contrib import admin, messages
from django.utils.translation import gettext_lazy as _

from documents.api import create_pad
from .forms import EventForm, FetMeetingForm, NewsForm, PostForm
from .models import Event, FetMeeting, FileUpload, News, Post


def make_fetmeeting(self, request, queryset):
    qs = self.get_queryset(request).filter(id=request.POST["_selected_action"]).first()

    agenda_key = create_pad(qs.slug + "-agenda")
    if not agenda_key:
        self.message_user(
            request,
            _("Das Agenda konnte nicht erstellt werden. Error: %s") % str(e),
            messages.ERROR,
        )
        return

    protocol_key = create_pad(qs.slug + "-protocol")
    if not protocol_key:
        self.message_user(
            request,
            _("Das Protokoll konnte nicht erstellt werden. Error: %s") % str(e),
            messages.ERROR,
        )
        return

    queryset.update(
        post_type="F",
        has_agenda=True,
        has_protocol=True,
        agenda_key=agenda_key,
        protocol_key=protocol_key,
    )
    self.message_user(
        request,
        _("Das Event %s wurde erfolgreich in eine FET Sitzung konvertiert.")
        % (qs.title),
        messages.SUCCESS,
    )


make_fetmeeting.short_description = "In eine Fachschaftssitzung konvertieren"


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


class PostAdmin(admin.ModelAdmin):
    form = PostForm
    model = Post
    list_filter = ["is_pinned", "status"]
    list_display = ["title", "slug", "public_date", "status", "is_pinned"]
    ordering = ["-public_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):
        if obj.author is None:
            obj.author = request.user
        super().save_model(request, obj, form, change)

    class Media:
        css = {
            "all": [
                "jquery-ui/jquery-ui.min.css",
                "jquery-ui/ui-lightness/theme.css",
            ]
        }
        js = [
            "jquery-ui/jquery-ui.min.js",
        ]


class NewsAdmin(PostAdmin):
    form = NewsForm
    model = News
    inlines = (FileUploadInline,)


class EventAdmin(PostAdmin):
    form = EventForm
    model = Event
    list_filter = ["is_pinned", "status"]
    list_display = ["title", "slug", "event_start", "status", "is_pinned"]
    ordering = ["-event_start"]
    actions = [make_fetmeeting]


class FetMeetingAdmin(EventAdmin):
    form = FetMeetingForm
    model = FetMeeting
    list_filter = []
    list_display = ["title", "slug", "event_start"]
    actions = []


admin.site.register(Event, EventAdmin)
admin.site.register(News, NewsAdmin)
admin.site.register(FetMeeting, FetMeetingAdmin)
