from django import template
from django.utils.safestring import mark_safe
from django.template.defaultfilters import stringfilter
from django.utils.translation import gettext_lazy as _

from urllib.parse import urlparse
from datetime import timedelta, date, datetime
from dateutil.relativedelta import relativedelta
from django.urls import reverse_lazy
from django.templatetags.static import static
from string import ascii_uppercase as auc
from django.utils.html import strip_tags
import emoji

register = template.Library()

@register.filter
def is_facebook_url(url):
    if url is None:
        return False
    obj = urlparse(url)
    if obj and obj.hostname:
        return obj.hostname.endswith("facebook.com")
    else:
        return False

@register.filter
def hostname(url):
    if url is None:
        return None
    obj = urlparse(url)
    if obj is None:
        return url
    hostname = obj.hostname
    if hostname:
        return mark_safe(hostname)
    else:
        return url


@register.filter
def add_de(txt):
    return ("d'" if txt[0].lower() in ["a", "e", "i", "o", "u", "y"] else "de ") + txt


@register.filter
def week(d):
    return d.isocalendar()[1]

@register.filter
def weekyear(d):
    return d.isocalendar()[0]

@register.filter
def not_before_first(d):
    return d >= datetime.now().date() - relativedelta(years=1)

@register.filter
def not_after_last(d):
    return d <= datetime.now().date() + relativedelta(years=1)

@register.filter
def shift_day(d, shift):
    return d + timedelta(days=shift)


@register.filter
def first_day_of_this_week(d):
    return date.fromisocalendar(d.year, week(d), 1)


@register.filter
def last_day_of_this_week(d):
    return date.fromisocalendar(d.year, week(d), 7)


@register.filter
def calendar_classes(d, fixed_style):
    result = ""
    if not fixed_style:
        if d.is_in_past():
            result += " past"
        if d.is_today():
            result += " today"
    if not d.on_requested_interval:
        result += " other_month"
    return result


@register.filter
def url_day(d, category=None):
    if category:
        return reverse_lazy(
            "day_view_category", kwargs={"year": d.year, "month": d.month, "day": d.day, "cat": category.slug}
        )
    else:
        return reverse_lazy(
            "day_view", kwargs={"year": d.year, "month": d.month, "day": d.day}
        )


@register.simple_tag
def picto_from_name(name, datatooltip=""):
    result = (
        '<svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">'
        + '<use href="'
        + static("images/feather-sprite.svg")
        + "#"
        + name
        + '" />'
        + "</svg>"
    )
    if datatooltip != "":
        result = '<span data-tooltip="' + datatooltip + '">' + result + "</span>"

    return mark_safe(result)


@register.filter
def int_to_abc(d):
    return auc[int(d)]


@register.filter(is_safe=True)
@stringfilter
def truncatechars_middle(value, arg):
    try:
        ln = int(arg)
    except ValueError:
        return value
    if len(value) <= ln:
        return value
    else:
        return "{}...{}".format(value[: ln // 2], value[-((ln + 1) // 2) :])


@register.filter
def frdate(d):
    return ("!" + d).replace(" 1 ", " 1er ").replace("!1 ", "!1er ")[1:]


@register.filter
def get_item(dictionary, key):
    return dictionary.get(key)


@register.filter
def remove_id_prefix(value):
    return int(value.replace("id_", ""))

@register.filter
def tocoords(c):
    return str(c.coords[1]) + ", " + str(c.coords[0])

@register.filter
def index(indexable, i):
    return indexable[i]

@register.filter
def is_string(val):
    return isinstance(val, str)

@register.filter
def html_vide(val):
    return len(strip_tags(val).replace("&nbsp;", "").strip()) == 0

@register.filter
def no_emoji(text):
    return emoji.replace_emoji(text, replace='')

@register.simple_tag
def navigation_links(filter, category):
    extra = '?' + filter.get_url()
    if category is None:
        result = '<li><a href="' + reverse_lazy('a_venir') + extra + '">maintenant</a></li>'
        result += '<li><a href="' + reverse_lazy('cette_semaine') + extra + '">cette semaine</a></li>'
        result += '<li><a href="' + reverse_lazy('ce_mois_ci') + extra + '">ce mois-ci</a></li>'
    else:
        result = '<li><a href="' + reverse_lazy('a_venir_category', kwargs={'cat': category.slug }) + extra + '">maintenant</a></li>'
        result += '<li><a href="' + reverse_lazy('cette_semaine_category', kwargs={'cat': category.slug }) + extra + '">cette semaine</a></li>'
        result += '<li><a href="' + reverse_lazy('ce_mois_ci_category', kwargs={'cat': category.slug }) + extra + '">ce mois-ci</a></li>'
    return mark_safe(result)