{#
  Copyright (C) 2020-2021 CERN.
  Copyright (C) 2020-2021 Northwestern University.
  Copyright (C) 2021-2023 TU Wien.
  Copyright (C) 2021 Graz University of Technology.
  Copyright (C) 2021 New York University.

  Invenio RDM Records 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 use_theme_basic_template|default(true) %}
  {%- extends config.BASE_TEMPLATE %}
{% endif %}

{%- from "invenio_app_rdm/records/macros/files.html" import file_list_box, preview_file_box, media_file_list_box %}

{%- set title = record.metadata.title %}
{%- set metadata = record.metadata %}

{%- set can_manage_record = permissions is defined and (permissions.can_edit or permissions.can_review) %}

<!-- preview_submission_request is set to true when coming from a community submission request -->
{%- set is_preview_submission_request = preview_submission_request or false %}
{%- set show_record_management_menu = can_manage_record and (not is_preview or is_preview_submission_request) %}

{%- if record.parent.access.settings %}
  {%- set allow_user_requests = not current_user.is_anonymous and record.parent.access.settings.allow_user_requests %}
  {%- set allow_guest_requests = current_user.is_anonymous and record.parent.access.settings.allow_guest_requests %}
{%- endif %}

{%- block page_body %}
  <section id="banners" class="banners" aria-label="{{ _('Information banner') }}">

    <!-- COMMUNITY HEADER: hide it when displaying the submission request -->
    {% if not is_preview_submission_request %}
      {% if community %}
        {% set is_community_restricted = community.access.visibility == 'restricted' %}
        <div class="ui fluid container page-subheader-outer with-submenu compact ml-0-mobile mr-0-mobile">
          <div class="ui container page-subheader">
            <div class="page-subheader-element">
              <img
                class="ui rounded image community-header-logo has-placeholder"
                src="{{ community.links.logo }}"
                alt=""
              />
            </div>
            <div class="page-subheader-element">
              <a
                href="{{ url_for('invenio_app_rdm_communities.communities_detail', pid_value=community.slug) }}"> {{ community.metadata.title }}</a>
              {% if is_community_restricted %}
                <span class="ui label horizontal small access-status restricted rel-ml-1"
                      title="{{ _('Community visibility') }}"
                      data-tooltip="{{ _('The community is restricted to users with access.') }}"
                      data-inverted="">
                  <i class="icon ban" aria-hidden="true"></i> {{ _("Restricted") }}
                </span>
              {% endif %}
            </div>
          </div>
        </div>
      {% endif %}
    {% endif %}
    <!-- /COMMUNITY HEADER -->

    <!-- PREVIEW HEADER -->
    {% if is_preview %}
      <div class="ui info flashed bottom attached manage message">
        <div class="ui container">
          <div class="header">
            <i class="eye icon"></i>
            <strong>{{ _ ("Preview") }}</strong>
          </div>
          <p>
            {% if not is_draft %}
              {{ _("You are previewing a published record.") }}
            {% elif record.is_published %}
              {{ _("You are previewing changes that have not yet been published.") }}
            {% elif not record.is_published and record.versions.index == 1 %}
              {{ _("You are previewing a new record that has not yet been published.") }}
            {% elif not record.is_published and record.versions.index > 1 %}
              {{ _("You are previewing a new record version that has not yet been published.") }}
            {% endif %}
          </p>
        </div>
      </div>
    {% endif %}
    <!-- /PREVIEW HEADER -->

    {% if record.is_published and record.links.latest_html and not record.versions.is_latest %}
      <div class="ui warning flashed bottom attached manage message">
        <div class="ui container">
          <div class="ui relaxed grid">
            <div class="column">
              <div class="row">
                <p>
                  {% trans link_start=('<a href="' + record.links.latest_html + '"><b>')|safe, link_end='</b></a>'|safe %}
                    There is a {{ link_start }}newer version{{ link_end }} of the record available.
                  {% endtrans %}
                </p>
              </div>
            </div>
          </div>
        </div>
      </div>
    {% endif %}
  </section>


  <div class="ui container">
    <div class="ui relaxed grid mt-5">
      <div class="two column row top-padded">

        {# Main content #}
        <article class="sixteen wide tablet eleven wide computer column main-record-content">
          {% if record.access.record == 'restricted' %}
            <section id="record-access-status"
                     class="ui {{ record.ui.access_status.message_class }} message rel-mt-1"
                     aria-label="{{ _('Record access information') }}"
            >
              <i class="{{ record.ui.access_status.icon }} icon" aria-hidden="true"></i>
              <strong>{{ record.ui.access_status.title_l10n }}</strong>
              {{ record.ui.access_status.description_l10n }}

              {% if record.access.embargo.reason %}
                <p>{{ _("Reason") }}: {{ record.access.embargo.reason }}</p>
              {% endif %}
            </section>
          {% endif %}

          {%- block record_body -%}

            {%- block record_header -%}

              {%- block record_header_button -%}

                {% if is_preview and not is_preview_submission_request and can_manage_record and is_draft %}
                  <nav class="back-navigation rel-pb-2 pl-0" aria-label="{{ _('Back-navigation') }}">
                    {%- set back_page = url_for('invenio_app_rdm_records.deposit_edit', pid_value=record.id) -%}
                    {%- set back_btn_label = _('Back to edit') -%}
                    <a class="ui button labeled icon small compact" href="{{ back_page }}">
                      <i class="ui icon angle left"></i> {{ back_btn_label }}</a>
                  </nav>
                {% endif %}

              {% endblock record_header_button %}

              {% if show_record_management_menu %}
                <section id="mobile-record-management" class="ui grid tablet only mobile only">
                  <div class="sixteen wide column right aligned">
                    <button id="manage-record-btn"
                            class="ui small basic icon button m-0"
                            aria-haspopup="dialog"
                            aria-expanded="false"
                    >
                      <i class="cog icon"></i> {{ _('Manage record') }}
                    </button>
                  </div>

                  <div id="recordManagementMobile"
                       role="dialog"
                       class="ui flowing popup transition hidden"
                       data-record='{{ record | tojson }}'
                       data-permissions='{{ permissions | tojson }}'
                       data-is-draft="{{ is_draft | tojson }}"
                  >
                  </div>
                </section>
              {% endif %}

              <section id="record-info" aria-label="{{ _('Publication date and version number') }}">
                <div class="ui grid middle aligned">
                  <div class="two column row">
                    <div class="left floated left aligned column">
                      <span class="ui" title="{{ _('Publication date') }}">
                        {{ _('Published') }} {{ record.ui.publication_date_l10n_long }}
                      </span>
                      <span
                        class="label text-muted"> | {{ _('Version {version_number}').format(version_number=record.ui.version) }}</span>
                    </div>
                    <div class="right floated right aligned column">

                      {% if record.ui.resource_type %}
                        <span role="note"
                              class="ui label horizontal small neutral mb-5"
                              aria-label="{{ _('Resource type') }}"
                        >
                          {{ record.ui.resource_type.title_l10n }}
                        </span>
                      {% endif %}

                      <span role="note"
                            class="ui label horizontal small access-status {{ record.ui.access_status.id }} mb-5"
                            data-tooltip="{{ record.ui.access_status.description_l10n }}"
                            data-inverted=""
                            aria-label="{{ _('Access status') }}"
                      >
                        {% if record.ui.access_status.icon %}
                          <i class="icon {{ record.ui.access_status.icon }}" aria-hidden="true"></i>
                        {% endif %}
                        <span aria-label="{{ record.ui.access_status.description_l10n }}">
                          {{ record.ui.access_status.title_l10n }}
                        </span>
                      </span>
                    </div>
                  </div>
                </div>
              </section>
              <div class="ui divider hidden"></div>

            {%- endblock record_header -%}

            {# Title #}
            {%- block record_title -%}
              <section id="record-title-section" aria-label="{{ _('Record title and creators') }}">
                <h1 id="record-title" class="wrap-overflowing-text">{{ metadata.title }}</h1>

                {% if record.ui.creators or record.ui.contributors %}
                  <section id="creatibutors" aria-label="{{ _('Creators and contributors') }}">
                    {%- include "invenio_app_rdm/records/details/creatibutors.html" %}
                  </section>
                {% endif %}
              </section>
            {%- endblock record_title -%}

            {# Description #}
            {%- block record_content -%}
              {%- include "invenio_app_rdm/records/details/description.html" %}
            {%- endblock record_content -%}

            {# Files #}
            {%- block record_files -%}
              {# record has files BUT passed files are empty. This happens when we display are request. #}
              {%- if record.files.enabled -%}
                <section id="record-files" class="rel-mt-2 rel-mb-3" aria-label="{{ _('Files') }}">
                  <h2 id="files-heading">{{ _('Files') }}</h2>
                  {%- if permissions.can_read_files -%}
                    {# record has files AND user can see files #}
                    {%- set files = files|order_entries %}

                    {%- if files|has_previewable_files -%}
                      {%-set preview_file = files|select_preview_file(default_preview=record.files.default_preview) %}
                      {{ preview_file_box(preview_file, record.id, is_preview, record) }}
                    {%- endif -%}
                    {{ file_list_box(files, record.id, is_preview, record) }}
                  {% else %}
                    {# record has files BUT user does not have permission to see files #}
                    <div class="ui accordion panel mb-10 {{ record.ui.access_status.id }}"
                          href="#files-preview-accordion-panel">
                      <h3 class="active title panel-heading {{ record.ui.access_status.id }} m-0">
                        <div role="button" id="files-preview-accordion-trigger" tabindex="0" class="trigger" aria-controls="files-preview-accordion-panel">
                          {{ _("Files") }}
                          <i class="angle right icon" aria-hidden="true"></i>
                        </div>
                      </h3>
                      <div role="region" id="files-preview-accordion-panel" aria-labelledby="files-preview-accordion-trigger" class="active content preview-container pt-0">
                        <div class="ui {{ record.ui.access_status.message_class }} message file-box-message">
                          <i class="ui {{ record.ui.access_status.icon }} icon"></i><b>{{ record.ui.access_status.title_l10n }}</b>
                          <p>{{ record.ui.access_status.description_l10n }}</p>

                          {% if record.access.embargo.reason %}
                            <p>{{ _("Reason") }}: {{ record.access.embargo.reason }}</p>
                          {% endif %}

                          {% block record_files_access_request %}
                            {%- if allow_user_requests or allow_guest_requests %}
                              <div class="ui divider"></div>
                              <p>
                                <p>
                                  <strong>{{ _("Request access") }}</strong>
                                </p>
                                <p>
                                  {{ _("If you would like to request access to these files, please fill out the form below.") }}
                                </p>
                                <div>
                                  {%- if record.parent.access.settings %}{%- set accept_conditions_text = record.parent.access.settings.accept_conditions_text %}{%- endif %}
                                  {%- if accept_conditions_text %}
                                  <p class="ui small header">{{ _("You need to satisfy these conditions in order for this request to be accepted:") }}</p>
                                  <p>
                                      {{ accept_conditions_text | safe }}
                                    </p>
                                  {%- endif %}
                                </div>
                                {%- include "invenio_app_rdm/records/details/access-form.html" %}
                              </p>
                            {%- endif %}
                          {% endblock record_files_access_request %}
                        </div>
                      </div>
                    </div>
                  {%- endif %}
                </section>
              {%- endif %}
            {%- endblock record_files -%}

            {# Media files #}
            {%- block record_media_files -%}
              {# record has media files AND user can see files #}
              {# can_media_read_files is false when record is fully restricted and users can't see the landing page at all #}
              {%- if media_files and media_files.enabled and media_files.entries and permissions.can_media_read_files -%}
                <section id="record-media-files" aria-label="{{ _('System files') }}">
                  {%- set media_files = media_files|order_entries %}
                  {{ media_file_list_box(media_files, record.id, is_preview, record) }}
                </section>
              {%- endif %}
            {%- endblock record_media_files -%}

            {# Additional details #}
            {%- block record_details -%}
              <section id="additional-details" class="rel-mt-2" aria-label="{{ _('Additional record details') }}">
                {%- include "invenio_app_rdm/records/details/details.html" %}
              </section>
            {%- endblock record_details -%}

            {# Record footer #}
            {%- block record_footer -%}
            {%- endblock record_footer -%}

          {%- endblock record_body %}
        </article>

        {# Sidebar #}
        <aside class="sixteen wide tablet five wide computer column sidebar" aria-label="{{ _('Record details') }}">
          {% block record_sidebar %}
            {%- include "invenio_app_rdm/records/details/side_bar.html" %}
          {% endblock record_sidebar %}
        </aside>
      </div>
    </div>

    <div class="ui container">
      <div class="ui relaxed grid">
        <div class="two column row">
          <div class="sixteen wide tablet eleven wide computer column">
            {# Jump up button #}
            {%- block jump -%}
              <div class="ui grid">
                <div class="centered row rel-mt-1">
                  <button id="jump-btn" class="jump-to-top ui button labeled icon" aria-label="{{ _('Jump to top of page') }}">
                    <i class="arrow alternate circle up outline icon"></i>
                    {{ _("Jump up") }}
                  </button>
                </div>
              </div>
            {%- endblock jump -%}
          </div>
        </div>
      </div>
    </div>
  </div>

{%- endblock page_body %}

{%- block javascript %}
  {% if use_theme_basic_template|default(true) %}
    {{ super() }}
  {% else %}
    {% include config.THEME_JAVASCRIPT_TEMPLATE %}
  {% endif %}
  {{ webpack['invenio-app-rdm-landing-page-theme.js'] }}
  {{ webpack['invenio-app-rdm-landing-page.js'] }}
  {{ webpack['previewer_theme.js'] }}
{%- endblock javascript %}
