from wagtail.contrib.modeladmin.options import ModelAdmin, ModelAdminGroup, modeladmin_register
from wagtail.core import hooks

from .models import BasicKnowledge, InstrumentCategory, InstrumentType
from django.utils.translation import ugettext_lazy as _
from core.logger import get_logger

logger = get_logger(__name__)

class InstrumentAdmin(ModelAdmin):
    model = BasicKnowledge
    list_display = ('title', 'new_type', 'status_string')
    search_fields = ('title', 'new_type__name')


class InstrumentCategoryAdmin(ModelAdmin):
    model = InstrumentCategory
    list_display = ('name', 'background', 'foreground')


class InstrumentTypeAdmin(ModelAdmin):
    model = InstrumentType
    list_display = ('name', 'category',)
    inspect_view_enabled = True
    inspect_view_fields = ('name', 'category', 'instruments',)


class InstrumentGroup(ModelAdminGroup):
    menu_label = _('Instruments')
    items = (InstrumentAdmin, InstrumentTypeAdmin, InstrumentCategoryAdmin,)


modeladmin_register(InstrumentGroup)


@hooks.register('construct_page_chooser_queryset')
def order_by_created(pages, request):
    logger.debug('constructing page chooser queryset')
    return pages.live().order_by('-latest_revision_created_at')
