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

  This file is part of Invenio.
  Copyright (C) 2016-2020 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.
#}

{%- macro preview_file(preview_endpoint, pid, filename, id='preview-iframe', width='100%', height='400' ) %}
  <iframe
   class="preview-iframe"
   id="{{id}}"
   width="{{width}}"
   height="{{height}}"
   src="{{ url_for(preview_endpoint, pid_value=pid.pid_value, filename=filename) }}"></iframe>
{%- endmacro %}

{%- macro file_list(files, pid, sort_by='key', with_preview=true, download_endpoint='invenio_records_ui.recid_files', preview_endpoint='invenio_records_ui.recid_previewer') %}
  <table class="ui striped table">
    <thead>
      <tr>
        <th>{{_('Name')}}</th>
        <th>{{_('Size')}}</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
    {% for file in files|sort(attribute=sort_by) %}
      {%- set file_url_download = url_for(download_endpoint, pid_value=pid.pid_value, filename=file.key, download=1) %}
      {%- set file_url_preview = url_for(preview_endpoint, pid_value=pid.pid_value, filename=file.key) %}
      {%- set file_type = file.key.split('.')[-1] %}
      <tr>
        <td>
          <a href="{{ file_url_download }}">{{ file.key }}</a>
          <br/><small>{{file.checksum}} 
            <div class="ui icon inline-block" data-tooltip="{{_('This is the file fingerprint (checksum), which can be used to verify the file integrity.')}}">
              <i class="question circle checksum icon"></i>
            </div>
        </td>
        <td>{{ file.size|filesizeformat }}</td>
        <td class="right aligned">
          <span>
            {% if with_preview and file_type is previewable %}
            <a class="ui basic mini button" href="{{ file_url_preview }}">
              <i class="eye icon"></i> {{_("Preview")}}
            </a>
            {% endif %}
            <a class="ui basic mini button" href="{{ file_url_download }}">
              <i class="download icon"></i>
              {{_('Download')}}
            </a>
          </span>
        </td>
      </tr>
    {% endfor %}
    </tbody>
  </table>
{%- endmacro %}
