from datetime import datetime

from fastapi.templating import Jinja2Templates

templates = Jinja2Templates(directory="templates")


def number_format(input: float) -> str:
    return format(input, "0,.2f").replace(".", ":").replace(",", ".").replace(":", ",")


def date_format(input: datetime) -> str:
    if input is None:
        return ""
    return input.strftime("%d.%m.%Y")


def date_format2(input: datetime) -> str:
    if input is None:
        return ""
    if isinstance(input, str):
        return input[:10]
    return input.strftime("%Y-%m-%d")


def checked(input: str) -> str:
    if input in ("J", "1", 1, True):
        return "checked"
    return ""


def selected(input: str) -> str:
    if input in ("J", "1", 1, True):
        return "selected"
    return ""


def truefalse(input: str) -> str:
    if input in ("J", "1", 1, True):
        return "true"
    return "false"


def show(input: str) -> str:
    if input in ("J", "1", 1, True):
        return "show"
    return ""


templates.env.filters["number_format"] = number_format
templates.env.filters["date_format"] = date_format
templates.env.filters["date_format2"] = date_format2
templates.env.filters["checked"] = checked
templates.env.filters["selected"] = selected
templates.env.filters["truefalse"] = truefalse
templates.env.filters["show"] = show
