from django.utils.translation import gettext_lazy as _
from wagtail.admin.ui.tables import Column, DateColumn
from wagtail.admin.ui.tables.pages import PageStatusColumn, PageTitleColumn
from wagtail.admin.viewsets.pages import PageListingViewSet, PageViewSet

from associations.models import AssociationPage
from dnscms.admin import ListingRedirectChooseParentView


class AssociationTypeColumn(Column):
    def get_value(self, instance):
        return instance.get_association_type_display()


class AssociationChooseParentView(ListingRedirectChooseParentView):
    listing_url_name = "associations:index"


class AssociationListingMixin:
    """Shared model + columns for the standalone listing and the page explorer."""

    model = AssociationPage
    icon = "group"
    columns = [
        PageTitleColumn("title", label=_("Title"), sort_key="title", classname="title"),
        AssociationTypeColumn(
            "association_type",
            label=_("Type"),
            sort_key="association_type",
            width="15%",
        ),
        DateColumn(
            "latest_revision_created_at",
            label=_("Updated"),
            sort_key="latest_revision_created_at",
            width="10%",
        ),
        PageStatusColumn("status", label=_("Status"), sort_key="live", width="10%"),
    ]


class AssociationSidebarViewSet(AssociationListingMixin, PageListingViewSet):
    """Standalone 'Associations' sidebar entry, reached independently of the page tree."""

    choose_parent_view_class = AssociationChooseParentView
    menu_label = _("Associations")
    menu_order = 2
    add_to_admin_menu = True
    ordering = "title"


class AssociationExplorerViewSet(AssociationListingMixin, PageViewSet):
    """Applies the same columns when navigating into AssociationIndex via the page explorer."""


association_sidebar_viewset = AssociationSidebarViewSet("associations")
association_explorer_viewset = AssociationExplorerViewSet()
