# (c) cavaliba.com - home - home.py

from django.conf import settings

from app_home.models import DashboardApp

from .configuration_default import CONFIGURATION_DEFAULT

# --------------------------------------------
# Cavaliba Apps
# --------------------------------------------

ICON_MAP_MD2FA = {
    "home": "fa-home",
    "public": "fa-sun-o",
    "location_on": "fa-map-marker",
    "checklist": "fa-tasks",
    "person": "fa-user-plus",
    "group": "fa-users",
    "apps": "fa-firefox",
    "web_asset": "fa-firefox",
    "night_shelter": "fa-hospital-o",
    "account_balance": "fa-university",
    "computer": "fa-laptop",
    "dns": "fa-server",
    "lan": "fa-wifi",
    "database": "fa-database",
    "build": "fa-wrench",
    "admin_panel_settings": "fa-user-secret",
    "lock": "fa-user-secret",
    "key": "fa-key",
    "book": "fa-list",
    "bar_chart": "fa-list",
    "list": "fa-list",
    "add": "fa-plus",
    "remove": "fa-minus",
    "settings": "fa-gears",
    "table_chart": "fa-table",
}
ICON_MAP_FA2MD = {
    "fa-home": "home",
    "fa-sun-o": "public",
    "fa-map-marker": "location_on",
    "fa-tasks": "checklist",
    "fa-user-plus": "person",
    "fa-users": "group",
    "fa-firefox": "web_asset",
    "fa-hospital-o": "night_shelter",
    "fa-university": "account_balance",
    "fa-laptop": "computer",
    "fa-server": "dns",
    "fa-wifi": "lan",
    "fa-database": "database",
    "fa-wrench": "build",
    "fa-user-secret": "admin_panel_settings",
    "fa-key": "key",
    "fa-list": "list",
    "fa-plus": "add",
    "fa-minus": "remove",
    "fa-gears": "settings",
    "fa-cog": "settings",
    "fa-table": "table_chart",
    "fa-book": "book",
}


def get_cavaliba_apps():

    reply = []

    appnames = [i for i in CONFIGURATION_DEFAULT]
    for appname in appnames:
        appobj = DashboardApp.objects.filter(keyname=appname).first()
        if appobj:
            reply.append(appobj)

    return reply


def get_sidebar(aaa=None):
    """CTX Processor : sidebar entries ; filter: enabled + perm = True"""

    entries = DashboardApp.objects.all().prefetch_related("permission").order_by("order")

    paginated = []
    pagelist = []
    index = {}  # page => [class1, class2]

    for entry in entries:
        if not entry.is_enabled:
            continue

        entry.is_allowed = False
        try:
            if entry.permission.keyname in aaa["perms"]:
                entry.is_allowed = True
        except Exception:
            pass

        # page/order for UI
        # [  [page1, [item1, item22,  ...] , [ page2, [...] ] ,  ... ]

        entry.icon = convert_icon(entry.icon)

        if entry.keyname == "home":
            continue

        section = entry.sidebar_section
        if not section:
            continue
        if len(section) == 0:
            continue
        if section not in index:
            index[section] = []
            pagelist.append(section)
        index[section].append(entry)

    # paginate
    for p in pagelist:
        paginated.append([p, index[p]])

    return paginated


def convert_icon(icon_name):
    """Convert icon name depending on HTML Framework"""

    fwk = settings.CAVALIBA_HTML_FRAMEWORK

    if fwk == "":  # bootstrap / fontawsome
        if icon_name.startswith("fa-"):
            return icon_name
        # map MD to FA
        try:
            return ICON_MAP_MD2FA[icon_name]
        except Exception:
            return "fa-minus"

    else:  # tw4 / material design
        if icon_name.startswith("fa-"):
            # map FA > MD
            try:
                return ICON_MAP_FA2MD[icon_name]
            except Exception:
                return "question_mark"

    return icon_name
