{# @var craft \craft\web\twig\variables\CraftVariable #}

{% import _self as self %}

{% set devMode = craft.app.config.general.devMode %}

{% set classes = ['admin-bar'] %}
{% set styles = {} %}

{% if fixed ?? false %}
  {% set classes = classes|merge(['fixed']) %}
{% elseif sticky ?? false %}
  {% set classes = classes|merge(['sticky']) %}
{% endif %}

{% if position ?? false and position == 'bottom' %}
  {% set classes = classes|merge(['bottom']) %}
{% endif %}

{% if rtl ?? false or userLanguageRtl ?? false %}
  {% set classes = classes|merge(['rtl']) %}
{% endif %}

{% if mobileMode ?? false %}
  {% set classes = classes|merge(['vertical-fullscreen']) %}
{% endif %}

{% if toolbarToggle ?? false %}
  {% set styles = styles|merge({'--admin-bar-toolbar-toggle-display': 'inline-flex'}) %}
{% endif %}

{% set adminBarAttributes = {
  class: classes,
  'greeting-text': 'greeting-text'|t('admin-bar', params = { name: currentUser.friendlyName }, language = userLanguage),
  id: id ?? 'admin-bar',
  'logout-href': url(logoutUrl),
  'logout-label': 'logout-label'|t('admin-bar', language = userLanguage),
  'show-environment': devMode,
  'show-greeting': displayGreeting,
  'show-logout': displayLogout,
  style: styles,
} %}

{% if currentUser and (currentUser.photo ?? false) %}
  {% set adminBarAttributes = adminBarAttributes|merge({
    'avatar-alt': 'avatar-alt'|t('admin-bar', params = { name: currentUser.friendlyName }, language = userLanguage),
    'avatar-src': currentUser.photo.getUrl({
      mode: 'crop',
      width: 100,
      height: 100,
    }),
  }) %}
{% endif %}

{% if mobileMode %}
  {% set adminBarAttributes = adminBarAttributes|merge({
    'auto-toggle-vertical': '700',
    'toolbar-toggle': true,
  }) %}
{% endif %}

{% if toolbarToggle %}
  {% set adminBarAttributes = adminBarAttributes|merge({
    'toolbar-toggle': true,
  }) %}
{% endif %}

{% if toolbarToggleDraggable %}
  {% set adminBarAttributes = adminBarAttributes|merge({
    'toolbar-toggle-drag': true,
  }) %}
{% endif %}

{% set textElements = textElements ?? [] %}

{% if devMode %}
  {% set adminBarAttributes = adminBarAttributes|merge({ 'environment-description': 'environment-description'|t('admin-bar', language = userLanguage) }) %}

  {% set envLabelStyle = (theme in ['craft-cp', 'sakura', 'stark']) ? {} : { '--admin-bar-badge-color-bg': 'var(--admin-bar-environment-bg-color)' } %}

  {% set textElements = textElements|merge([{
    'badge-content': craft.app.config.env|upper,
    style: envLabelStyle,
  }]) %}
{% endif %}

<craft-admin-bar
    data-admin-bar="{{ adminBarAttributes.id }}"
    data-action-url="{{ actionUrl('admin-bar/admin-bar') }}"
    data-api-status="ready"
    data-session-action-url="{{ actionUrl('users/session-info') }}"
></craft-admin-bar>
<admin-bar {{ attr(adminBarAttributes) }}>
  {% if displayGreeting %}
    <nav slot="greeting-popover">
      <admin-bar-text multi-line {% if currentUser.admin %}badge-content="{{ 'greeting-badge-admin'|t('admin-bar', language = userLanguage) }}"{% endif %}>
        <div style="margin-inline-end: 1em;">
          <strong style="display: block">{{ currentUser.fullName }}</strong>
          <em style="display: block">{{ currentUser.username }}</em>
        </div>
      </admin-bar-text>

      <admin-bar-button button-href="{{ cpUrl('myaccount') }}" style="border-block-start: 1px solid color-mix(in srgb, var(--admin-bar-button-color-bg-hover), transparent 80%);">
        {{ 'label-greeting-profile'|t('admin-bar', language = userLanguage) }}
        <span slot="before-label">{{ self.iconUser }}</span>
      </admin-bar-button>

      <admin-bar-button button-href="{{ cpUrl('myaccount/addresses') }}">
        {{ 'label-greeting-addresses'|t('admin-bar', language = userLanguage) }}
        <span slot="before-label">{{ self.iconUserAddresses }}</span>
      </admin-bar-button>

      {% if currentUser.can('assignUserPermissions') %}
        <admin-bar-button button-href="{{ cpUrl('myaccount/permissions') }}">
          {{ 'label-greeting-permissions'|t('admin-bar', language = userLanguage) }}
          <span slot="before-label">{{ self.iconUserPermissions }}</span>
        </admin-bar-button>
      {% endif %}

      <admin-bar-button button-href="{{ cpUrl('myaccount/preferences') }}">
        {{ 'label-greeting-preferences'|t('admin-bar', language = userLanguage) }}
        <span slot="before-label">{{ self.iconUserPreferences }}</span>
      </admin-bar-button>

      {% if currentUser and currentUser.admin and proEdition %}
        <admin-bar-checkbox id="admin-bar-checkbox-debug-toolbar" {% if currentUser.getPreference('enableDebugToolbarForSite') %}checked{% endif %} input-switch>
          {{ 'label-greeting-debug-toolbar'|t('admin-bar', language = userLanguage) }}
          <span slot="checked-icon">{{ self.iconChecked }}</span>
          <span slot="unchecked-icon">{{ self.iconUnchecked }}</span>
        </admin-bar-checkbox>
      {% endif %}
    </nav>
  {% endif %}

  {% if displayDashboardLink ?? true %}
    <admin-bar-button button-href="{{ cpUrl('dashboard') }}" label-text="{{ 'label-dashboard'|t('admin-bar', language = userLanguage) }}"><span slot="before-label">{{ self.iconDashboard }}</span></admin-bar-button>
  {% endif %}

  {% if editLinkUrl ?? false %}
  <admin-bar-button button-href="{{ cpUrl(editLinkUrl) }}" label-text="{{ 'label-edit-name'|t('admin-bar', params = { name: editLinkLabel ?? false ? ' ' ~ editLinkLabel : '' }, language = userLanguage) }}"><span slot="before-label">{{ self.iconEdit }}</span></admin-bar-button>
  {% else %}
    {% if entry ?? false and ((userCanEdit ?? false) or (entry.section.id in craft.app.entries.editableSectionIds)) %}
      {% set sectionName = displayDefaultEditSection ? ' ' ~ entry.section : ''  %}
      {% set entryEditUrl = entry.cpEditUrl %}

      {% if (returnUrl is defined and returnUrl != null) or returnUrl is not defined %}
        {% set entryEditUrlFormatted = cpUrl(entryEditUrl, { returnUrl: (returnUrl ?? entry.url ?? craft.app.request.absoluteUrl)|hash }) %}
      {% else %}
        {% set entryEditUrlFormatted = cpUrl(entryEditUrl) %}
      {% endif %}

      <admin-bar-button button-href="{{ entryEditUrlFormatted }}" label-text="{{ 'label-edit-name'|t('admin-bar', params = { name: editLinkLabel ?? false ? ' ' ~ editLinkLabel : '' }, language = userLanguage) }}"><span slot="before-label">{{ self.iconEdit }}</span></admin-bar-button>
    {% endif %}
  {% endif %}

  {% if not 'guide' in widgets|keys and displayGuideLink and plugin('guide') and (currentUser ? currentUser.can('accessPlugin-guide') : true) %}
    <admin-bar-button button-href="{{ cpUrl('guide') }}" label-text="{{ 'label-guide'|t('admin-bar', language = userLanguage) }}"><span slot="before-label">{{ self.iconGuide }}</span></admin-bar-button>
  {% endif %}

  {% if customLinks ?? false %}
    {% for link in customLinks %}
      {% if link.linkUrl ?? false %}
        {% if link.adminOnly != true %}
          <admin-bar-button button-href="{{ url(link.linkUrl) }}" label-text="{{ link.linkLabel|t('admin-bar', language = userLanguage) }}">{{ (link.icon ?? false) ? self.customLinkIcon(link.icon) : '' }}</admin-bar-button>
        {% elseif link.adminOnly and currentUser and currentUser.admin %}
          <admin-bar-button button-href="{{ url(link.linkUrl) }}" label-text="{{ link.linkLabel|t('admin-bar', language = userLanguage) }}">{{ (link.icon ?? false) ? self.customLinkIcon(link.icon) : '' }}</admin-bar-button>
        {% endif %}
      {% endif %}
    {% endfor %}
  {% endif %}

  {% for link in additionalLinks %}
    {{ self.addConfigLink(link) }}
  {% endfor %}

  {% if not proEdition %}
    <span style="flex-grow: 1;"></span>
  {% endif %}

  {% if currentUser and displayUtilitiesLink ?? true %}
    <admin-bar-button
        button-href="{{ cpUrl('utilities') }}"
        label-text="{{ 'label-utilities'|t('admin-bar', language = userLanguage) }}"
        {% if utilitiesNotificationCount > 0 %}badge-content="{{ utilitiesNotificationCount }}"{% endif %}
    >{% if not utilitiesNotificationCount %}<span slot="before-label">{{ self.iconUtilities }}</span>{% endif %}</admin-bar-button>
  {% endif %}

  {% if currentUser and currentUser.admin and displaySettingsLink ?? true %}
    <admin-bar-button button-href="{{ cpUrl('settings') }}" label-text="{{ 'label-settings'|t('admin-bar', language = userLanguage) }}"><span slot="before-label">{{ self.iconSettings }}</span></admin-bar-button>
  {% endif %}

  {% if proEdition %}
    <span style="flex-grow: 1;"></span>

    {% if widgets|length %}
      {% include 'admin-bar/_plugin-widgets.twig' with {
        displayWidgetLabels,
        enabledWidgets,
        entry,
        userLanguage,
        widgets,
        widgetsConfig,
      } only %}
    {% endif %}

    {% if customWidgets ?? false %}
      {{ include(template_from_string(customWidgets)) }}
    {% endif %}

    {% if globalCustomWidgets ?? false %}
      {{ include(template_from_string(globalCustomWidgets)) }}
    {% endif %}
  {% endif %}

  {% if textElements|length %}
    {% for textAttrs in textElements %}
      <admin-bar-text {{ attr(textAttrs) }}></admin-bar-text>
    {% endfor %}
  {% endif %}
</admin-bar>

{% if useCss %}
  {% css adminBarOnPageCss() %}
{% endif %}

{% macro addConfigLink(link) %}
  {% if link.linkParams ?? false %}
    {% set params = link.linkParams %}
  {% else %}
    {% set params = '' %}
  {% endif %}

  {% set params = link.params ?? '' %}
  {% set protocol = link.protocol ?? '' %}
  {% set mustShowScriptName = link.mustShowScriptName ?? '' %}

  {% set userHasPermission = true %}
  {% if link.admin ?? false %}
    {% if currentUser and not currentUser.admin %}
      {% set userHasPermission = false %}
    {% endif %}
  {% elseif link.permissions ?? false %}
    {% for permission in link.permissions %}
      {% if currentUser and not currentUser.can(permission) %}
        {% set userHasPermission = false %}
      {% endif %}
    {% endfor %}
  {% endif %}

  {% if link.type == 'url' and userHasPermission %}
    <admin-bar-button button-href="{{ url(link.url, params, protocol, mustShowScriptName) }}" label-text="{{ link.title|t('admin-bar', language = userLanguage) }}">{{ (link.icon ?? false) ? self.customLinkIcon(link.icon) : '' }}</admin-bar-button>
  {% elseif link.type == 'cpUrl' and userHasPermission %}
    <admin-bar-button button-href="{{ cpUrl(link.url, params, protocol, mustShowScriptName) }}" label-text="{{ link.title|t('admin-bar', language = userLanguage) }}">{{ (link.icon ?? false) ? self.customLinkIcon(link.icon) : '' }}</admin-bar-button>
  {% elseif link.type == 'action' and userHasPermission %}
    {% set actionParams = params|length ? params ~ '&returnUrl=' ~ craft.request.url : 'returnUrl=' ~ craft.request.url %}
    <admin-bar-button button-href="{{ actionUrl(link.url, actionParams) }}" label-text="{{ link.title|t('admin-bar', language = userLanguage) }}">{{ (link.icon ?? false) ? self.customLinkIcon(link.icon) : '' }}</admin-bar-button>
  {% endif %}
{% endmacro %}

{% macro customLinkIcon(icon) %}{% apply spaceless %}
  {% set iconSvg = svg('@appicons/' ~ icon ~ '.svg')|attr({ class: 'icon' }) %}

  {% if iconSvg ?? false %}
    <span slot="before-label">{{ iconSvg|raw }}</span>
  {% endif %}
{% endapply %}{% endmacro %}

{# dashboard #}
{% macro iconDashboard() %}{% apply spaceless %}
  {{ svg('@appicons/gauge.svg')|attr({ class: 'icon' }) }}
{% endapply %}{% endmacro %}

{# checkboxes #}
{% macro iconChecked() %}{% apply spaceless %}
  {{ svg('@appicons/square-check.svg')|attr({ class: 'icon' }) }}
{% endapply %}{% endmacro %}

{% macro iconUnchecked() %}{% apply spaceless %}
  {{ svg('@appicons/square.svg')|attr({ class: 'icon' }) }}
{% endapply %}{% endmacro %}

{# edit #}
{% macro iconEdit() %}{% apply spaceless %}
  {{ svg('@appicons/pencil.svg')|attr({ class: 'icon' }) }}
{% endapply %}{% endmacro %}

{# guide #}
{% macro iconGuide() %}{% apply spaceless %}
  {{ svg('@wbrowar/guide/assetbundles/static/icon-mask.svg')|attr({ class: 'icon' }) }}
{% endapply %}{% endmacro %}

{# utilities #}
{% macro iconUtilities() %}{% apply spaceless %}
  {{ svg('@appicons/wrench.svg')|attr({ class: 'icon' }) }}
{% endapply %}{% endmacro %}

{# settings #}
{% macro iconSettings() %}{% apply spaceless %}
  {{ svg('@appicons/gear.svg')|attr({ class: 'icon' }) }}
{% endapply %}{% endmacro %}

{# user #}
{% macro iconUser() %}{% apply spaceless %}
  {{ svg('@appicons/address-card.svg')|attr({ class: 'icon' }) }}
{% endapply %}{% endmacro %}

{% macro iconUserPermissions() %}{% apply spaceless %}
  {{ svg('@appicons/key-skeleton.svg')|attr({ class: 'icon' }) }}
{% endapply %}{% endmacro %}

{% macro iconUserPreferences() %}{% apply spaceless %}
  {{ svg('@appicons/swatchbook.svg')|attr({ class: 'icon' }) }}
{% endapply %}{% endmacro %}

{% macro iconUserAddresses() %}{% apply spaceless %}
  {{ svg('@appicons/map-location.svg')|attr({ class: 'icon' }) }}
{% endapply %}{% endmacro %}

{# utilities #}
{% macro iconUtilities() %}{% apply spaceless %}
  {{ svg('@appicons/wrench.svg')|attr({ class: 'icon' }) }}
{% endapply %}{% endmacro %}
