{% comment %}
Copyright (C) 2017-2025  The Software Heritage developers
See the AUTHORS file at the top-level directory of this distribution
License: GNU Affero General Public License version 3, or any later version
See top-level LICENSE file for more information
{% endcomment %}

{% load swh_templatetags %}

{% if vault_cooking.directory_context or vault_cooking.revision_context %}
  {% if user.is_authenticated and user.is_staff or "swh.vault.git_bare.ui" in user.get_all_permissions %}
    <div class="btn-group">
      <button class="btn btn-secondary btn-sm dropdown-toggle swh-vault-download"
              type="button"
              data-bs-toggle="dropdown">
        <i class="mdi mdi-download mdi-fw" aria-hidden="true"></i>
        Download
      </button>
      <div class="dropdown-menu swh-vault-menu">
        {% if vault_cooking.directory_context %}
          <button class="dropdown-item"
                  type="button"
                  tabindex="-1"
                  onclick="swh.vault.vaultRequest('directory', '{{ vault_cooking.directory_swhid }}')">
            <i class="{{ swh_object_icons.directory }} mdi-fw" aria-hidden="true"></i>
            as tarball
          </button>
        {% endif %}
        {% if vault_cooking.revision_context %}
          <button class="dropdown-item"
                  type="button"
                  tabindex="-1"
                  onclick="swh.vault.vaultRequest('revision', '{{ vault_cooking.revision_swhid }}')">
            <i class="{{ swh_object_icons.revision }} mdi-fw" aria-hidden="true"></i>
            as git
          </button>
        {% endif %}
      </div>
    </div>
  {% else %}
    <button class="btn btn-secondary btn-sm swh-vault-download"
            type="button"
            onclick="swh.vault.vaultRequest('directory', '{{ vault_cooking.directory_swhid }}')">
      <i class="mdi mdi-download mdi-fw" aria-hidden="true"></i>
      Download
    </button>
  {% endif %}
  <!-- modals related to the creation of vault cooking tasks -->
  <!-- they will be reparented in the script below in order to be able to display them -->
  <div class="modal fade"
       id="vault-cook-directory-modal"
       tabindex="-1"
       role="dialog"
       aria-labelledby="vault-cook-directory-modal-label"
       aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h6 class="modal-title" id="vault-cook-directory-modal-label">
            Cook and download a directory from the Software Heritage Vault
          </h6>
          <button type="button"
                  class="btn-close"
                  data-bs-dismiss="modal"
                  aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <p>
            You have requested the cooking of the directory with identifier <strong>{{ vault_cooking.directory_swhid }}</strong>
            into a standard <code>tar.gz archive</code>.
          </p>
          <p>Are you sure you want to continue ?</p>
          <form onsubmit="swh.vault.cookDirectoryArchive(event, '{{ vault_cooking.directory_swhid }}')">
            <div>
              <label class="form-label" for="email">
                (Optional) Send download link once it is available to that email address:
              </label>
              <input type="email" class="form-control" id="swh-vault-directory-email" />
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button"
                  class="btn btn-secondary btn-sm"
                  data-bs-dismiss="modal">Cancel</button>
          <button type="button"
                  class="btn btn-secondary btn-sm"
                  onclick="swh.vault.cookDirectoryArchive(event, '{{ vault_cooking.directory_swhid }}')">
            Ok
          </button>
        </div>
      </div>
    </div>
  </div>
  <div class="modal fade"
       id="vault-download-directory-modal"
       tabindex="-1"
       role="dialog"
       aria-labelledby="vault-download-directory-modal-label"
       aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h6 class="modal-title" id="vault-download-directory-modal-label">
            Download a directory from the Software Heritage Vault
          </h6>
          <button type="button"
                  class="btn-close"
                  data-bs-dismiss="modal"
                  aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <p>
            You have requested the download of the directory with identifier <strong>{{ vault_cooking.directory_swhid }}</strong>
            as a standard <code>tar.gz archive</code>.
          </p>
          <p>Are you sure you want to continue ?</p>
        </div>
        <div class="modal-footer">
          <button type="button"
                  class="btn btn-secondary btn-sm"
                  data-bs-dismiss="modal">Cancel</button>
          <button type="button"
                  class="btn btn-secondary btn-sm"
                  onclick="swh.vault.fetchDirectoryArchive('{{ vault_cooking.directory_swhid }}')">
            Ok
          </button>
        </div>
      </div>
    </div>
  </div>
  <div class="modal fade"
       id="vault-cook-revision-modal"
       tabindex="-1"
       role="dialog"
       aria-labelledby="vault-cook-revision-modal-label"
       aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h6 class="modal-title" id="vault-cook-revision-modal-label">
            Cook and download a revision from the Software Heritage Vault
          </h6>
          <button type="button"
                  class="btn-close"
                  data-bs-dismiss="modal"
                  aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <p>
            You have requested the cooking of the history heading to revision with identifier <strong>{{ vault_cooking.revision_swhid }}</strong>
            into a <code>bare git archive</code>.
          </p>
          <p>Are you sure you want to continue ?</p>
          <form onsubmit="swh.vault.cookRevisionArchive(event, '{{ vault_cooking.revision_swhid }}')">
            <div>
              <label class="form-label" for="email">
                (Optional) Send download link once it is available to that email address:
              </label>
              <input type="email" class="form-control" id="swh-vault-revision-email" />
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button"
                  class="btn btn-secondary btn-sm"
                  data-bs-dismiss="modal">Cancel</button>
          <button type="button"
                  class="btn btn-secondary btn-sm"
                  onclick="swh.vault.cookRevisionArchive(event, '{{ vault_cooking.revision_swhid }}')">
            Ok
          </button>
        </div>
      </div>
    </div>
  </div>
  <div class="modal fade"
       id="vault-download-revision-modal"
       tabindex="-1"
       role="dialog"
       aria-labelledby="vault-download-revision-modal-label"
       aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h6 class="modal-title" id="vault-download-revision-modal-label">
            Download a revision from the Software Heritage Vault
          </h6>
          <button type="button"
                  class="btn-close"
                  data-bs-dismiss="modal"
                  aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <p>
            You have requested the download of the history heading to revision with identifier <strong>{{ vault_cooking.revision_swhid }}</strong>
            as a <code>bare git archive</code>.
          </p>
          <p>Are you sure you want to continue ?</p>
        </div>
        <div class="modal-footer">
          <button type="button"
                  class="btn btn-secondary btn-sm"
                  data-bs-dismiss="modal">Cancel</button>
          <button type="button"
                  class="btn btn-secondary btn-sm"
                  onclick="swh.vault.fetchRevisionArchive('{{ vault_cooking.revision_swhid }}')">
            Ok
          </button>
        </div>
      </div>
    </div>
  </div>
  <div class="modal fade"
       id="invalid-email-modal"
       tabindex="-1"
       role="dialog"
       aria-labelledby="invalid-email-modal-label"
       aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="invalid-email-modal-label">Invalid Email !</h4>
          <button type="button"
                  class="btn-close"
                  data-bs-dismiss="modal"
                  aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <p>The provided email is not well-formed.</p>
        </div>
        <div class="modal-footer">
          <button type="button"
                  class="btn btn-secondary btn-sm"
                  data-bs-dismiss="modal">Ok</button>
        </div>
      </div>
    </div>
  </div>
  {% include "./vault-common.html" %}
{% elif vault_cooking.content_context %}
  {% comment %}
    Vault cannot cook a content object, in that case offer a direct file download link instead.
  {% endcomment %}

  <a href="{{ vault_cooking.content_download_url | safe }}"
     class="btn btn-secondary btn-sm"
     role="button">
    <i class="mdi mdi-download mdi-fw" aria-hidden="true"></i>
    Download
  </a>
{% endif %}
