# *-* coding: utf-8 -*-

import logging

from django import template
from django.core.urlresolvers import reverse
#from django.utils.translation import ugettext_lazy as _

from ..models.nonrel import Read

LOGGER = logging.getLogger(__name__)

register = template.Library()


@register.simple_tag()
def read_status_css(read):

    css = []

    for attr in Read.get_status_attributes():
        css.append(attr if getattr(read, attr) else (u'not_'+attr))

    return u' '.join(css)


@register.simple_tag()
def read_action_toggle_url(read):

    any_key = Read.get_status_attributes()[0]
    url_base = reverse('toggle', kwargs={'klass': 'Read',
                       'oid': read.id, 'key': any_key}).replace(
                        any_key, u'@@KEY@@')

    return u'data-url-action-toggle={0}'.format(url_base)


@register.inclusion_tag('snippets/read/read-action.html')
def read_action(read, action_name, with_text=True, popover_direction=None):

    action_value = getattr(read, action_name)

    return {
        'with_text': with_text,
        'popover_direction' : popover_direction or 'top',
        'action_name': action_name,
        'action_data' : Read.status_data.get(action_name),
        'popover_class': '' if with_text else 'popover-tooltip',
        'do_start_hidden' : 'hide' if action_value else '',
        'undo_start_hidden' : '' if action_value else 'hide',
        'js_func': "toggle_status('{0}', '{1}')".format(
                   read.id, action_name)

    }


@register.inclusion_tag('snippets/read/read-action-status.html')
def read_action_status(read, action_name, with_text=False):

    return {
        'with_text': with_text,
        'action_name': action_name,
        'action_data' : Read.status_data.get(action_name),
        'status_hidden' : '' if getattr(read, action_name) else 'hide',
    }

@@ -165,16 +165,93 @@
 y as _%0A%0A
+%0Afrom ...base.utils.dateutils import now, naturaldelta as onef_naturaldelta%0A%0A
 from ..m
@@ -342,24 +342,127 @@
 Library()%0A%0A%0A
+@register.filter%0Adef naturaldelta(the_datetime):%0A%0A    return onef_naturaldelta(now() - the_datetime)%0A%0A%0A
 @register.si
@@ -461,34 +461,32 @@
 ister.simple_tag
-()
 %0Adef read_status

Add the natural delta template filter.