{% comment %}
Copyright (C) 2017-2026  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 %}

{% include "./revision-info.html" %}
{% if snapshot_context and snapshot_context.is_empty %}
  {% include "./empty-snapshot.html" %}
{% else %}
  {% if not iframe_mode %}
    <div class="card">
      <div class="swh-content-filename card-header swh-background-gray swh-heading-color">
        {% if filename %}{{ filename }}{% endif %}
        <div class="ms-auto float-end swh-preview-code-switch d-none js-only">
          <div class="btn-group" role="group">
            <input type="radio"
                   class="btn-check"
                   name="preview-code-switch"
                   id="code-switch"
                   autocomplete="off"
                   checked>
            <label class="btn btn-secondary btn-sm"
                   for="code-switch"
                   id="code-switch-label"
                   onclick="swh.content.switchToCode()">Code</label>
            <input type="radio"
                   class="btn-check"
                   name="preview-code-switch"
                   id="preview-switch"
                   autocomplete="off">
            <label class="btn btn-secondary btn-sm"
                   id="preview-switch-label"
                   for="preview-switch"
                   onclick="swh.content.switchToPreview()">Preview</label>
          </div>
        </div>
      </div>
    {% endif %}
    <div class="swh-content js-only">
      {% if content_size > max_content_size %}
        Content is too large to be displayed (size is greater than {{ max_content_size|filesizeformat }}).
      {% elif "application/pdf" == mimetype %}
        <div class="text-center">
          <div class="py-2">
            <button class="btn btn-secondary btn-sm" id="pdf-prev">Previous</button>
            <span>Page: <span id="pdf-page-num"></span> / <span id="pdf-page-count"></span></span>
            <button class="btn btn-secondary btn-sm" id="pdf-next">Next</button>
          </div>
          <canvas id="pdf-canvas"></canvas>
        </div>
      {% elif filename|is_notebook %}
        <div class="swh-ipynb swh-preview-content"></div>
        <div class="highlightjs">
          <pre><code class="{{ language }}">{{ content }}</code></pre>
        </div>
      {% elif filename|is_markdown or "text/html" == mimetype %}
        <iframe sandbox="allow-scripts allow-same-origin"
                class="swh-html-content swh-preview-content"
                style="width: 100%;
                       height: 100vh">
        </iframe>
        <div class="highlightjs">
          <pre><code class="{{ language }}">{{ content }}</code></pre>
        </div>
      {% elif "inode/x-empty" == mimetype %}
        <i>File is empty</i>
      {% elif mimetype in browsers_supported_image_mimes and content %}
        <img src="data:{{ mimetype }};base64,{{ content }}" alt="image" />
      {% elif textual_content %}
        <div class="highlightjs">
          <pre><code class="{{ language }}">{{ content }}</code></pre>
        </div>
      {% elif content %}
        Content with mime type {{ mimetype }} and encoding {{ encoding }} cannot be displayed.
      {% else %}
        {% include "includes/http-error.html" %}
      {% endif %}
      {% if content %}
        {{ filename|swh_json_script:"filename" }}
        {{ mimetype|swh_json_script:"mimetype" }}
        {{ top_right_link.url|swh_json_script:"content_url" }}
        {{ mathjax_js_library|swh_json_script:"mathjax_js_library" }}
        {{ iframe_mode|swh_json_script:"iframe_mode" }}
        <script>
          swh.content.renderContent(
            swh.webapp.parseJsonScript('filename'),
            swh.webapp.parseJsonScript('mimetype'),
            swh.webapp.parseJsonScript('content_url'),
            swh.webapp.parseJsonScript('mathjax_js_library'),
            !swh.webapp.parseJsonScript('iframe_mode')
          );
        </script>
      {% endif %}
    </div>
    <noscript>
      {% if content_size > max_content_size %}
        Content is too large to be displayed (size is greater than {{ max_content_size|filesizeformat }}).
      {% elif "inode/x-empty" == mimetype %}
        <i>File is empty</i>
      {% elif mimetype in browsers_supported_image_mimes and content %}
        <img src="data:{{ mimetype }};base64,{{ content }}" alt="image" />
      {% elif textual_content %}
        <iframe src="{% url 'browse-content-highlight' 'sha1_git:'|add:sha1_git %}?filename={{ filename }}"
                id="swh-code-highlight-iframe"
                style="width: 100%;
                       height: {{ no_script_iframe_height }}">
        </iframe>
      {% elif content %}
        Content with mime type {{ mimetype }} and encoding {{ encoding }} cannot be displayed.
      {% else %}
        {% include "includes/http-error.html" %}
      {% endif %}
    </noscript>
    {% if not iframe_mode %}</div>{% endif %}
{% endif %}
