from django import template
from datetime import datetime

from panel.helper_excel import shamsi_date, to_locale_str

register = template.Library()


@register.filter(name='jalali')
def jalali(value):
    if not value:
        return ""

    try:
        if isinstance(value, str):
            try:
                value = datetime.fromisoformat(value)
            except:
                return '-'

        if hasattr(value, 'date'):
            value = value.date()

        return shamsi_date(value,in_value=True)
    except:
        return '-'


@register.filter(name='isNone')
def is_none(value):
    if value is None:
        return "-"
    return value


@register.filter(name='toLocaleStr')
def to_locale_string(value):
    if value is None:
        return "-"
    return to_locale_str(value)


@register.filter(name='int')
def to_int(value):
    if value is None:
        return "-"
    return int(value)
