"""Admin classes"""

from django.contrib import admin
from django.conf import settings
from tagging.models import Tag

from models import Location, Poi, Narrative
from rldjango.common.admin import TreeEditor


class TreeModelAdmin(TreeEditor): # pylint: disable-msg=R0904
    """
    Base admin class from mptt models.
    """

    def _actions_column(self, item):
        """
        Add some useful actions: add child, 
        """
        actions = super(TreeModelAdmin, self)._actions_column(item)

        temp = (u'<a href="add/?parent=%s" title="%s" '
            'onclick="return showAddAnotherPopup(this);">'
            '<img src="%simg/admin/icon_addlink.gif" alt="%s"/></a>')
        actions.append(temp % (item.pk, 'Add child page',
            settings.ADMIN_MEDIA_PREFIX , 'Add child page'))
        
        return actions
    
class PoiModelAdmin(admin.ModelAdmin): # pylint: disable-msg=R0904
    """
    Base admin model class for poi model
    """
    search_fields = ['name', 'description', 'address', 'tags']
    list_display = ('name', 'location', 'category', 'language', 'actions_column', 'tags')
    list_filter = [ 'category', 'language', 'location']

    def actions_column(self, poi): # pylint: disable-msg=R0201
        """
        Add custom actions for poi objects
        """

        actions = []

        actions.append((
            ("<a href='/destinfo/poi2pdf/%d/' alt='Generate pdf.' "
             "<img src='/media/destinfo/img/pdf_logo.png'/></a>" % poi.id
            )))
        
        return '\n'.join(actions)
    actions_column.short_description = 'Actions' # pylint: disable-msg=W0612
    actions_column.allow_tags = True # pylint: disable-msg=W0612

class PoisInline(admin.TabularInline):
    """
    Used to add pois to Narrative
    """
    model = Narrative.pois.through # pylint: disable-msg=E1101

class TagInline(admin.TabularInline):
    """
    Used to add pois to Narrative
    """
    model = Narrative.poi_tags.through # pylint: disable-msg=E1101


class NarrativeModelAdmin(TreeModelAdmin):
    """
    Narrative ModelAdmin needs poi inlines
    """
    search_fields = ['name', ]
    inlines = [PoisInline]
    exclude = ['pois']
    filter_horizontal = ['poi_tags']
admin.site.register(Poi, PoiModelAdmin)
admin.site.register(Location, TreeModelAdmin)
admin.site.register(Narrative, NarrativeModelAdmin)

    
