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

import re
import decimal

from gsxws import objectify

import babel.numbers
from datetime import timedelta

from django import template
from django.utils import safestring, timezone
from django.utils.translation import ugettext as _
from django.template.defaultfilters import date
from django.contrib.humanize.templatetags.humanize import naturaltime

from servo.models.common import Configuration

register = template.Library()

def gsx_diags_ts(value):
    return objectify.gsx_diags_timestamp(value)

def unread_notifications(queryset):
    queryset = queryset.filter(action='set_status', handled_at=None)
    return queryset.order_by('-handled_at')

def unread_messages(queryset):
    queryset = queryset.filter(action='note_added', handled_at=None)
    return queryset.order_by('-handled_at')

def count_or_empty(queryset):
    if queryset.count() > 0:
        return queryset.count()
    return ''

def str_find(string, substr):
    return (string.find(substr) > -1)

@register.filter(expects_localtime=True, is_safe=False)
def relative_date(value):
    if value in ('', None):
        return ''

    current = timezone.now()
    if (current - value) > timedelta(days=1):
        return date(value, "SHORT_DATETIME_FORMAT")
    return naturaltime(value)

def highlight(text, string):
    result = re.sub(r'('+string+')', '<span class="highlight">\g<0></span>', text)
    return result

def amount_in_location(obj, user):
    Returns how many instances of this product
    are at the current user's location
    return obj.get_amount_stocked(user)

def is_item_complete(obj, item):
    return obj.is_item_complete(item)

def item_completed_by(obj, item):
    item = is_item_complete(obj, item)

        return item.checked_by.username
    except Exception:
        return ''

def widget_is(obj, widget):
        return obj.field.widget.__class__.__name__ == widget
    except AttributeError:
        return False

def currency(value):
        c = Configuration.conf('currency')
        return babel.numbers.format_currency(decimal.Decimal(value), c)
    except Exception:
        return value

def active(request, *args):
    s = '/'.join([str(i) for i in args])
    pattern = s[:-1] if s.endswith('//') else s
    path = request.path.lstrip('/')
    return 'active' if re.search(pattern, path) else ''

def active_url(request, url):
    return 'active' if request.path == url else ''

def paginator_page(request, page):
    query = request.GET.copy()
    if 'page' in query.keys():
        del query['page']
    query['page'] = page
    return query.urlencode()

def markdown(text):
    import markdown
    result = markdown.markdown(text)
    return safestring.mark_safe(result)

def concat(str1, str2):
    return str(str1) + str(str2)

def addspace(s):
    return str(s).replace(',', ', ')

def device_accessories(device, order):
    return device.get_accessories(order)

def replace(value, arg):
    old, new = arg.split(",")
    return value.replace(old, new)