"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 05.07.21

"""
from django import template

# Create custom library
from django.utils.html import format_html

from news.models import ServerMessageImportance

register = template.Library()

# ServerMessageImportance bootstrap resolver
SVI_BOOTSTRAP_CLS_MAP = {
    ServerMessageImportance.DEFAULT: "",
    ServerMessageImportance.WARNING: "alert-danger",
    ServerMessageImportance.INFO: "alert-info",
}


@register.filter("bootstrap_cls")
def bootstrap_cls(value):
    """ Returns a bootstrap html class name

    Resolves ServerMessageImportance enum into a html class name

    Args:
        value ():
        arg ():

    Returns:

    """
    return SVI_BOOTSTRAP_CLS_MAP.get(value, "")


@register.filter("default_if_zero")
def default_if_zero(val1, val2):
    """ Returns val2 if val1 is 0

    Similar to default_if_none

    Args:
        val1 (int): The numerical value
        val2 (str): The alternative

    Returns:

    """
    return val1 if val1 > 0 else val2


@register.filter("shorten")
def shorten(val, length):
    """ Returns val shortened to the first number of length character

    Args:
        val (str): The value
        length (int): The number of characters left

    Returns:

    """
    if val is not None and len(val) > length:
        val = f"{val[:length]}..."
    return val
