{# -*- coding: utf-8 -*-

  This file is part of Invenio.
  Copyright (C) 2015-2021 CERN.

  Invenio is free software; you can redistribute it and/or modify it
  under the terms of the MIT License; see LICENSE file for more details.
#}


{%- if config.ACCOUNTS %}
  {%- if not current_user.is_authenticated %}
    <form>
      <a
        href="{{ url_for_security('login', next=request.path) }}"
        class="ui button">
        <i class="sign-in icon"></i>
        {{ _('Log in') }}</a>
      {% if security.registerable %}
        <a href="{{ url_for_security('register') }}" class="ui button signup">
          <i class="edit outline icon"></i>
          {{ _('Sign up') }}
        </a>
      {% endif %}
    </form>
  {%- else %}

    {# Desktop Quick-create ("plus") menu #}
    {%- set plus_menu_items = current_menu.submenu('plus').children %}
    {%- if plus_menu_items %}
      <div
        id="quick-create-dropdown"
        class="rdm-plus-menu rdm-plus-menu-responsive ui dropdown floating pr-15 computer only"
      >
        <a
          id="quick-create-dropdown-btn"
          role="button"
          aria-controls="quick-create-menu"
          aria-label="{{ _('Quick create') }}"
          aria-expanded="false"
          aria-haspopup="menu"
        >
          <i class="fitted plus icon inverted" aria-hidden="true"></i>
          <i class="fitted dropdown icon inverted" aria-hidden="true"></i>
        </a>

        <div role="menu" aria-labelledby="quick-create-dropdown-btn" id="quick-create-menu" class="menu">
          {%- for item in plus_menu_items if item.visible %}
            <a role="menuitem" class="item" href="{{ item.url }}">{{ item.text|safe }}</a>
          {%- endfor %}
        </div>
      </div>

      {# Mobile/Tablet Quick create ("plus") menu #}
      <div class="sub-menu mobile tablet only">
        <h2 class="ui small header">{{ _("Actions") }}</h2>
        <div role="menu">
          {%- for item in plus_menu_items if item.visible %}
            <a role="menuitem" class="item" href="{{ item.url }}">
              <i class="plus icon"></i>
              {{ item.text|safe }}
            </a>
          {%- endfor %}
        </div>
      </div>
    {% endif %}

    {# Desktop Account dropdown menu #}
    {%- if config.USERPROFILES %}
      <div id="user-profile-dropdown" class="ui floating dropdown computer only">
        <button
          id="user-profile-dropdown-btn"
          class="ui right labeled right floated icon button text"
          aria-controls="user-profile-menu"
          aria-expanded="false"
          aria-haspopup="menu"
          aria-label="{{ _('My account') }}"
        >
          <span>
            <i class="user icon" aria-hidden="true"></i>
            {{ current_user.email|truncate(31,true) }}
          </span>
          <i class="dropdown icon" aria-hidden="true"></i>
        </button>

        <div
          id="user-profile-menu"
          class="ui menu"
          role="menu"
          aria-labelledby="user-profile-dropdown-btn"
        >
          {%- for item in current_menu.submenu('settings').children if item.visible %}
            <a role="menuitem" class="item" href="{{ item.url }}" tabindex="-1">
              {{ item.text|safe }}
            </a>
          {%- endfor %}

          <div class="ui divider"></div>

          {% set ns = namespace(admin_menu=False) %}
          {%- for item in current_menu.submenu('profile-admin').children if item.visible %}
            {% set ns.admin_menu = True %}
            <a role="menuitem" class="item" href="{{ item.url }}" tabindex="-1">
              {{ item.text|safe }}
            </a>
          {%- endfor %}
          {% if ns.admin_menu %}
            <div class="ui divider"></div>
          {% endif %}

          <a role="menuitem" class="item" href="{{ url_for_security('logout') }}" tabindex="-1">
            <i class="sign-out icon"></i>
            {{ _('Log out') }}
          </a>
        </div>
      </div>

      {# Mobile/Tablet Account menu #}
      <div class="sub-menu mobile tablet only">
        <h2 class="ui small header">{{ _("My account") }}</h2>

        <div role="menu">
          {%- for item in current_menu.submenu('settings').children if item.visible %}
            <a role="menuitem" class="item" href="{{ item.url }}">
              {{ item.text|safe }}
            </a>
          {%- endfor %}

          <div class="ui divider"></div>

          {% set ns = namespace(admin_menu=False) %}
          {%- for item in current_menu.submenu('profile-admin').children if item.visible %}
            {% set ns.admin_menu = True %}
            <a role="menuitem" class="item" href="{{ item.url }}">
              {{ item.text|safe }}
            </a>
          {%- endfor %}

          {% if ns.admin_menu %}
            <div class="ui divider"></div>
          {% endif %}

          <a role="menuitem" class="item" href="{{ url_for_security('logout') }}">
            <i class="sign-out icon"></i>
            {{ _('Log out') }}
          </a>
        </div>
      </div>

    {%- else %}
      <a role="button" href="{{ url_for_security('logout') }}" class="ui button">
        <i class="sign-out icon"></i>
        {{ _('Log out') }}
      </a>
    {%- endif %}
  {%- endif %}
{%- endif %}
