{% comment %}
Copyright (C) 2017-2021  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 %}
  {% 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-default btn-sm dropdown-toggle swh-vault-download" type="button" data-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-default 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="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </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>
            <div class="form-group">
              <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-default btn-sm" data-dismiss="modal">Cancel</button>
          <button type="button" class="btn btn-default btn-sm" onclick="swh.vault.cookDirectoryArchive('{{ vault_cooking.directory_swhid }}')">Ok</button>
        </div>
      </div>
    </div>
  </div>

  <div class="modal fade" id="vault-fetch-directory-modal" tabindex="-1" role="dialog" aria-labelledby="vault-fetch-directory-modal-label" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h6 class="modal-title" id="vault-fetch-directory-modal-label">Download a directory from the Software Heritage Vault</h6>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </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-default btn-sm" data-dismiss="modal">Cancel</button>
          <button type="button" class="btn btn-default 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="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </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>
            <div class="form-group">
              <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-default btn-sm" data-dismiss="modal">Cancel</button>
          <button type="button" class="btn btn-default btn-sm" onclick="swh.vault.cookRevisionArchive('{{ vault_cooking.revision_swhid }}')">Ok</button>
        </div>
      </div>
    </div>
  </div>

  <div class="modal fade" id="vault-fetch-revision-modal" tabindex="-1" role="dialog" aria-labelledby="vault-fetch-revision-modal-label" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h6 class="modal-title" id="vault-fetch-revision-modal-label">Download a revision from the Software Heritage Vault</h6>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </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-default btn-sm" data-dismiss="modal">Cancel</button>
          <button type="button" class="btn btn-default 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">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h4 class="modal-title" id="invalid-email-modal-label">Invalid Email !</h4>
        </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-default btn-sm" data-dismiss="modal">Ok</button>
        </div>
      </div>
    </div>
  </div>

  {% include "includes/vault-common.html" %}

{% endif %}
