{#
    Nav
    --
    These macros are designed for building the primary navigation and user
    navigation.

    By default, the prime nav is generated by the nav system in the python. It
    is strongly recommended that any navigation links are added/removed using
    python, however if required, you can overide the prime navigation in your
    local template like so:
--------------------------------------------------------------------------------
{%- import "macros/nav.html" as _nav -%}

{% block prime_nav -%}
    {% macro prime_nav_list() %}
        <div class="mh-nav__group">
            <div class="mh-nav__item">
                <span class="mh-nav__link">
                    <span class="mh-nav__link-text">Links</span>
                </span>

                <div class="mh-nav__sub-list  mh-nav__sub-list--2">
                    <div class="mh-nav__item">
                        <a class="mh-nav__link" href="/some-url-1">
                            <span class="mh-nav__link-text">Link 1</span>
                        </a>
                    </div>

                    <div class="mh-nav__item">
                        <a class="mh-nav__link" href="/some-url-2">
                            <span class="mh-nav__link-text">Link 2</span>
                        </a>
                    </div>
                </div>
            </div>

            <div class="mh-nav__item">
                <a class="mh-nav__link" href="/some-url">
                    <span class="mh-nav__link-text">Link 3</span>
                </a>
            </div>

            ...
        </div>

        ...
    {% endmacro %}

    {{ _nav.prime_nav(prime_nav_list()) }}
{%- endblock %}
--------------------------------------------------------------------------------
    Taking this approach allows you to retain the prime nav container formatting
    but overwrite the inner list of nav items/links.

    The user nav component is very similar to the prime nav component in it's
    HTML/CSS construction but is not generated using python. By default, the
    user's name, the update profile link and the sign out link are added, as
    these are standard behaviour in any CMS, however, if required, you can
    overwrite these links like so:
--------------------------------------------------------------------------------
{% block user_nav -%}
    {% macro user_nav_list() %}
        ...
    {% endmacro %}

    {{ _nav.user_nav(user_nav_list()) }}
{%- endblock %}
--------------------------------------------------------------------------------
#}

{#
    Prime nav
    --
    The containing element for the `prime_nav` component. Must be passed `list`
    as a mixin which should contain the list of links in for the navigation.
#}
{% macro prime_nav(list) -%}
    <div class="[ mh-nav__prime-nav ]  [ mh-prime-nav ]">
        <div
            class="[ mh-nav__handle ]  [ mh-prime-nav__handle ]"
            data-open-frame-class="mh-frame--prime-nav-open"
            >
            <div class="mh-prime-nav__handle-box">
                <div class="mh-prime-nav__handle-dash"></div>
            </div>
        </div>

        <div class="[ mh-nav__list ]  [ mh-prime-nav__list ]">
            {{ list }}
        </div>
    </div>
{%- endmacro %}

{#
    Nav list
    --
    A mixin used in conjunction with the python generated navigation items to
    work out whether the item passed is a parent or child element in the
    navigation tree structure.
#}
{% macro nav_list(item) -%}
    {% if item.parent -%}
        {{ nav_item(item) }}
    {%- else -%}
        {% for child in item.children -%}
            {{ nav_item(child) }}
        {%- endfor %}
    {%- endif %}
{%- endmacro %}

{#
    Nav item
    --
    A mixin used in conjunction with the python generated navigation items to
    build the container for a navigation item and it's sub-list if it is a
    parent item.
#}
{% macro nav_item(item) -%}
    <div
        class="
            mh-nav__item
            {{ 'mh-nav__item--has-sub-list' if item.is_parent }}
            {{ 'mh-nav__item--open' if item.is_parent and item.is_open }}
        "
        >
        {{ nav_link(item) }}

        {% if item.is_parent -%}
            <div
                class="
                    mh-nav__sub-list
                    mh-nav__sub-list--{{ item.children|length }}
                "
                >
                {% for child in item.children -%}
                    {{ nav_list(child) }}
                {%- endfor %}
            </div>
        {%- endif %}
    </div>
{%- endmacro %}

{#
    Nav link
    --
    A mixin used in conjunction with the python generated navigation items to
    build the link element.
#}
{% macro nav_link(item) -%}
    {% if item.endpoint -%}
        {% set link = item.query %}

        {% if link.allowed -%}
            <a
                class="
                    mh-nav__link
                    {{ 'mh-nav__link--active' if item.is_active }}
                "
                href="{{ link.url }}"
                >
                {{ nav_link_text(item.label, item.badge) }}
            </a>
        {%- else -%}
            <span class="mh-nav__link  mh-nav__link--disabled">
                {{ nav_link_text(item.label, item.badge) }}
            </span>
        {%- endif %}
    {%- elif item.fixed_url -%}
        <a
            class="mh-nav__link"
            href="{{ item.fixed_url }}"
            target="_blank"
            >
            {{ nav_link_text(item.label, item.badge) }}
        </a>
    {%- else -%}
        <span class="mh-nav__link">
            {{ nav_link_text(item.label, item.badge) }}
        </span>
    {%- endif %}
{%- endmacro %}

{#
    Nav link text
    --
    A mixin used in conjunction with the python generated navigation items to
    build the text and badge (if applicable) for the nav link.
#}
{% macro nav_link_text(text, badge) -%}
    <span class="mh-nav__link-text">
        {{ text }}
    </span>

    {% if badge in [True, False] -%}
        <span class="mh-nav__link-badge"></span>
    {%- elif badge -%}
        <span class="mh-nav__link-count">
            {{ badge if badge < 100 else '...' }}
        </span>
    {%- endif %}
{%- endmacro %}

{#
    User nav
    --
    The containing element for the `user_nav` component. Must be passed `list`
    as a mixin which should contain the list of links in for the navigation.
    Must also be passed the current `user` (`g.me`) which is used to obtain and
    display the user's name at the top of the user nav component.
#}
{% macro user_nav(list, user) -%}
    <div class="[ mh-nav__user ]  [ mh-user-nav ]">
        <div
            class="[ mh-nav__handle ]  [ mh-user-nav__handle ]"
            data-open-frame-class="mh-frame--user-nav-open"
            >
            <div class="mh-user-nav__handle-icon"></div>
            <div class="mh-user-nav__handle-image"></div>

            <div class="mh-user-nav__handle-name">
                {{ user.first_name }}
                <br>
                {{ user.last_name }}
            </div>
        </div>

        <div class="[ mh-nav__list ]  [ mh-user-nav__list ]">
            <div class="mh-user-nav__list-name">
                {{ user.full_name }}
            </div>

            {{ list }}
        </div>
    </div>
{%- endmacro %}

{#
    User nav list
    --
    The list of navigation links displayed in the user nav component.
#}
{% macro user_nav_list() -%}
    <div class="mh-nav__group">
        <div class="mh-nav__item">
            <a
                class="mh-nav__link"
                href="{{ url_for('users.manage__update_my_profile') }}"
                >
                <span class="mh-nav__link-text">
                    Update my profile
                </span>
            </a>
        </div>
    </div>

    <div class="mh-nav__group">
        <div class="mh-nav__item">
            <a
                class="mh-nav__link"
                href="{{ url_for('users.manage__sign_out') }}"
                >
                <span class="mh-nav__link-text  mh-nav__link-text--sign-out">
                    Sign-out
                </span>
            </a>
        </div>
    </div>
{%- endmacro %}