{# ::
Sidebar create a stacked sidebar menu
It brings the whole navigation to sidebar
It can be used with along with navbar to only show just the active menu set
:: #}

{# :: Show only the active menu set :: #}
{% macro active_menu(panel="default", class_="") %}
    {{ menu(use_active=True, panel=panel, class_=class_) }}
{% endmacro %}

{# :: Show menu with a set key name :: #}
{% macro key_menu(key, panel="default", class_="") %}
    {{ menu(key=key, panel=panel, class_=class_) }}
{% endmacro %}


{# :: MENU general
@params key: str - The key name of the menu, to display only this set
@param use_active: bool - When True, it will select the active menu and buil the menu on it
@param panel: str - with bootstrap 3, it can be use to change the panel
@param class_: str - A class name to add to the menu
:: #}
{% macro menu(key=None, use_active=False, panel="default", class_="") %}

<div class="panel panel-{{ panel }} {{ class_ }}">

    {% if use_active %}

        {% for nav in __g.NAV_MENU %}
            {% set nav_name = nav[1] %}
            {% set nav_items = nav[2] %}
            {% set nav_kwargs = nav[3] %}

            {% if nav_kwargs["__active"] %}

                {% if nav_items %}

                    {{ _sub_menu(nav) }}

                {% endif %}

            {% endif %}

        {% endfor %}


    {% elif key %}

        {% for nav in __g.NAV_MENU %}
            {% set nav_name = nav[1] %}
            {% set nav_items = nav[2] %}
            {% set nav_kwargs = nav[3] %}

            {% if nav_kwargs["key"] == key %}

                {% if nav_items %}

                    {{ _sub_menu(nav) }}

                {% endif %}

            {% endif %}

        {% endfor %}

    {% else %}

        {% for nav in __g.NAV_MENU %}
            {% set nav_name = nav[1] %}
            {% set nav_items = nav[2] %}
            {% set nav_kwargs = nav[3] %}

            {% if nav_items %}
                {{ _sub_menu(nav) }}
            {% endif %}
        {% endfor %}

    {% endif %}




</div>

{% endmacro %}


{% macro _sub_menu(nav) %}
    {% set nav_name = nav[1] %}
    {% set nav_items = nav[2] %}
    {% set nav_kwargs = nav[3] %}

    <div class="panel-heading" role="tab" id="collapseListGroupHeading{{ nav_kwargs.__index }}">
    <h4 class="panel-title">
      <a class="" role="button" data-toggle="collapse"
         href="#collapseListGroup{{ nav_kwargs.__index }}"
         aria-expanded="true"
         aria-controls="collapseListGroup{{ nav_kwargs.__index }}">
        {{ nav_name }}
      </a>
    </h4>
    </div>

    <div id="collapseListGroup{{ nav_kwargs.__index }}" class="panel-collapse collapse {% if nav_kwargs.__active %}in{% endif %}"
       role="tabpanel" aria-labelledby="collapseListGroupHeading{{ nav_kwargs.__index }}" aria-expanded="true">

        {{ _listgroup(nav_items) }}
    </div>
{% endmacro %}

{% macro _listgroup(nav_items) %}

    <ul class="list-group">

      {% for item in nav_items %}
          {% set item_name = item[1] %}
          {% set item_endpoint = item[2] %}
          {% set item_kwargs = item[3] %}
          <li class="list-group-item
          {% if item_endpoint == request.endpoint %}active{% endif %}
          "><a href="{{ url_for(item_endpoint) }}">{{ item_name }}</a></li>
      {% endfor %}

    </ul>

{% endmacro %}