from django.contrib import admin
from newsroom.models import *
from newsroom.forms import *
from django.contrib.contenttypes import generic

# from genericadmin.admin import GenericAdminModelAdmin
from markitup.widgets import AdminMarkItUpWidget


class AuthorAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("name",)}
    list_display = (
        "type",
        "name",
        "created",
        "user",
    )


admin.site.register(Author, AuthorAdmin)


class DatelineAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("title",)}


admin.site.register(Dateline, DatelineAdmin)


class EditorialTypeAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("name",)}


admin.site.register(EditorialType, EditorialTypeAdmin)


class EditorialAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("print_hed",)}


admin.site.register(Editorial, EditorialAdmin)


class ArchiveSectionAdmin(admin.ModelAdmin):
    save_on_top = True
    prepopulated_fields = {"slug": ("title",)}
    filter_horizontal = (
        "stories",
        "galleries",
        "photos",
        "pdf_documents",
        "editorials",
    )
    date_hierarchy = "published_on"
    list_filter = (
        "paper",
        "dateline",
    )
    search_fields = (
        "title",
        "body",
    )


admin.site.register(ArchiveSection, ArchiveSectionAdmin)
admin.site.register(ArchiveStory)
admin.site.register(ArchiveDocument)
admin.site.register(ArchiveMovie)
admin.site.register(ArchiveSlideshow)


class AttachedLinkInline(generic.GenericStackedInline):
    extra = 1
    model = AttachedLink


class AttachedDocumentInline(generic.GenericStackedInline):
    extra = 1
    model = AttachedDocument


class AttachedGraphicInline(generic.GenericStackedInline):
    extra = 1
    model = AttachedGraphic


class AttachedGalleryInline(generic.GenericStackedInline):
    extra = 1
    model = AttachedGallery


class StoryAdmin(admin.ModelAdmin):
    save_on_top = True
    raw_id_fields = ["lead_photo"]
    actions_selection_counter = True
    inlines = [
        AttachedGalleryInline,
        AttachedGraphicInline,
        AttachedLinkInline,
        AttachedDocumentInline,
    ]
    prepopulated_fields = {"slug": ("web_hed",)}
    list_display = (
        "web_hed",
        "dateline",
        "created",
        "published",
        "created_by",
        "view_count",
    )
    date_hierarchy = "published_on"
    filter_horizontal = (
        "authors",
        "towns",
        "sites",
        "paper_editions",
    )
    list_filter = (
        "dateline",
        "published",
        "sites",
    )
    search_fields = (
        "web_hed",
        "body",
        "authors__name",
    )
    fieldsets = [
        (
            None,
            {
                "fields": [
                    ("web_hed", "print_hed"),
                    ("slug", "standing_hed"),
                    ("kicker", "subhed"),
                    "authors",
                    "dateline",
                    "body",
                    "markup",
                    "lead_photo",
                ]
            },
        ),
        ("Meta", {"fields": ["summary", ("auto_tag", "tags")]}),
        (
            "Placement",
            {
                "fields": [
                    "weight",
                    "paper_editions",
                    "status",
                    ("published", "published_on"),
                    "towns",
                    "sites",
                ]
            },
        ),
    ]

    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == "body":
            kwargs["widget"] = AdminMarkItUpWidget()
        return super(StoryAdmin, self).formfield_for_dbfield(db_field, **kwargs)

    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user
        obj.save()


admin.site.register(Story, StoryAdmin)

# class PageAdmin(admin.ModelAdmin):
#    save_on_top=True
#    search_fields=('ad_notes', 'ad_spec_pos',)
#    list_display = ('edition','number','type',)
#    list_filter = ('type', 'edition',)

# admin.site.register(Page, PageAdmin)

admin.site.register(Link)
admin.site.register(Paper)


class WebEditionAdmin(admin.ModelAdmin):
    list_display = ('paper', 'published_on', 'published', 'site')
    list_filter = ('paper', 'published', 'site')
    list_per_page = 12
    raw_id_fields = ["featured_story", "featured_photo"]
    filter_horizontal = (
        "stories",
        "photos",
        "galleries",
        "editorials",
        "remembrances",
        "death_announcements",
        "memorial_services",
        "inmemoriams",
    )
    fieldsets = [
        (None, {"fields": ["paper", ("published", "published_on"), "site"]}),
        (
            "Editorial",
            {
                "fields": [
                    "featured_story",
                    "featured_photo",
                    "stories",
                    "editorials",
                    "pdfdocuments",
                ]
            },
        ),
        (
            "Multimedia",
            {
                "classes": ["collapse"],
                "fields": ["photos", "galleries"],
            },
        ),
        (
            "Obits",
            {
                "classes": ["collapse"],
                "fields": [
                    "remembrances",
                    "death_announcements",
                    "memorial_services",
                    "inmemoriams",
                ],
            },
        ),
    ]


admin.site.register(WebEdition, WebEditionAdmin)
admin.site.register(PaperEdition)
admin.site.register(AuthorType)
admin.site.register(StandingHed)
# admin.site.register(PageType)
