"""admin interface customisations"""

# pylint: disable=W0613

from django.conf import settings
from django.contrib import admin
from django.utils import timezone

from . import models
from . import utilities


@admin.action(description="Approve selected")
def approve(modeladmin, request, queryset):
    """approve selected"""
    queryset.update(approved=True)

    for instance in queryset:
        instance.announce()

        if hasattr(instance, "event"):  # CFP
            recipient = instance.event.contact_email
        if hasattr(instance, "contact_email"):
            recipient = instance.contact_email

        if recipient:
            if hasattr(instance, "name"):
                title = instance.name
            else:
                title = instance.title

            subject = f"✅ {title} has been approved on AusGLAMR!"
            message = f"<html><body><p>{title} has been approved on <a href='https://{settings.DOMAIN}'>AusGLAMR</a>. Hooray!</p>"
            if type(instance).__name__ == "Event":
                message += f"<p>You can now optionally <a href='https://{settings.DOMAIN}/register-cfp'>register a call for papers</a>.</p>"
            message += "</body></html>"

            utilities.send_email(subject, message, recipient)


@admin.action(description="Unapprove selected")
def unapprove(modeladmin, request, queryset):
    """unapprove selected"""
    queryset.update(approved=False)


@admin.action(description="Suspend selected blogs")
def suspend(modeladmin, request, queryset):
    """suspend selected blogs"""
    queryset.update(suspended=True)

    for instance in queryset:
        if hasattr(instance, "contact_email"):
            if hasattr(instance, "name"):
                title = instance.name
            else:
                title = instance.title

            subject = f"⚠️ {title} has been suspended from AusGLAMR!"
            message = f"<html><body>\
            <p>Your blog {title} has been suspended from AusGLAMR. It may be unsuspended in future once the issue is resolved. If you would like more information, please reply to this email.</p> \
            </body></html>"

            utilities.send_email(subject, message, instance.contact_email)


@admin.action(description="Unsuspend selected blogs")
def unsuspend(modeladmin, request, queryset):
    """unsuspend selected blogs"""
    queryset.update(suspended=False, suspension_lifted=timezone.now())

    for instance in queryset:
        if hasattr(instance, "contact_email"):
            if hasattr(instance, "name"):
                title = instance.name
            else:
                title = instance.title

            subject = f"✅ The AusGLAMR suspension on {title} has been lifted"
            message = f"<html><body>\
            <p>The suspension on your blog {title} has been removed on AusGLAMR. Please note that articles published whilst it was suspended will not be added to AusGLAMR retrospectively. If you would like more information, please reply to this email.</p> \
            </body></html>"

            utilities.send_email(subject, message, instance.contact_email)


@admin.action(description="Confirm selected subscribers")
def confirm(modeladmin, request, queryset):
    """confirm selected"""
    queryset.update(confirmed=True)


@admin.action(description="Unconfirm selected subscribers")
def unconfirm(modeladmin, request, queryset):
    """unconfirm selected"""
    queryset.update(confirmed=False)


@admin.action(description="Deactivate selected blogs")
def disable(modeladmin, request, queryset):
    """disable selected"""
    queryset.update(active=False)


@admin.action(description="Activate selected blogs")
def activate(modeladmin, request, queryset):
    """un-disable selected"""
    queryset.update(active=True)


@admin.action(description="Send confirmation request to selected subscribers")
def send_conf_request(modeladmin, request, queryset):
    """send to selected"""

    for instance in queryset:
        instance.send_confirmation_email()


@admin.register(models.Blog)
class Blog(admin.ModelAdmin):
    """display settings for blogs"""

    list_display = (
        "url",
        "title",
        "author_name",
        "approved",
        "announced",
        "suspended",
        "failing",
        "active",
    )
    ordering = ["approved", "-suspended", "-failing"]
    search_fields = ["title", "author_name", "url"]
    actions = [approve, unapprove, suspend, unsuspend, activate, disable]


@admin.register(models.Article)
class Article(admin.ModelAdmin):
    """display settings for articles"""

    date_hierarchy = "pubdate"
    list_display = ("title", "blog_title", "pubdate")
    search_fields = ["title", "author_name", "blog_title", "url"]

    def blog_title(self, obj):  # pylint: disable=no-self-use
        """get the title of the parent blog"""
        return obj.blog.title


@admin.register(models.Tag)
class Tag(admin.ModelAdmin):
    """display settings for tags"""

    list_display = ("name",)
    search_fields = ["name"]


@admin.register(models.Event)
class Event(admin.ModelAdmin):
    """display settings for conferences"""

    list_display = (
        "name",
        "approved",
        "announcements",
        "category",
        "description",
        "start_date",
    )
    ordering = ["approved", "announcements"]
    search_fields = ["name", "description", "url"]
    actions = [approve, unapprove]


@admin.register(models.CallForPapers)
class CallForPapers(admin.ModelAdmin):
    """display settings for CFPs"""

    list_display = ("name", "event", "approved", "closing_date")
    list_select_related = ("event",)
    ordering = ["approved", "closing_date"]
    search_fields = ["event__name", "event__url", "details"]
    actions = [approve, unapprove]


@admin.register(models.Group)
class Group(admin.ModelAdmin):
    """display settings for groups"""

    list_display = ("name", "approved", "category", "description")
    ordering = ["approved", "name"]
    search_fields = ["name", "description", "url"]
    actions = [approve, unapprove]


@admin.register(models.Newsletter)
class Newsletter(admin.ModelAdmin):
    """display settings for newsletters"""

    list_display = (
        "name",
        "approved",
        "announced",
        "failing",
        "active",
    )
    ordering = ["approved", "-failing"]
    search_fields = ["name", "description", "url", "author_name"]
    actions = [approve, unapprove, suspend, activate, disable]


@admin.register(models.Edition)
class Edition(admin.ModelAdmin):
    """display settings for editions"""

    date_hierarchy = "pubdate"
    list_display = ("title", "newsletter_name", "pubdate")
    search_fields = ["title", "description", "url", "newsletter__name", "author_name"]

    def newsletter_name(self, obj):  # pylint: disable=no-self-use
        """get the title of the parent newsletter"""
        return obj.newsletter.name


@admin.register(models.ContentWarning)
class ContentWarning(admin.ModelAdmin):
    """display settings for CWs"""

    list_display = (
        "match_text",
        "display",
    )


@admin.register(models.Announcement)
class Announcement(admin.ModelAdmin):
    """display settings for announcements"""

    list_display = ("status",)


@admin.register(models.SiteMessage)
class SiteMessage(admin.ModelAdmin):
    """create a message"""

    list_display = ("message",)


@admin.register(models.Subscriber)
class Subscriber(admin.ModelAdmin):
    """email subscribers"""

    list_display = (
        "email",
        "confirmed",
    )
    actions = [confirm]
