{% extends "layout/base.html" %}

{% block title %}{{ product.name }} definition{% endblock %}
{% set fixed_metadata = product_summary.fixed_metadata or {} %}

{% block panel %}

{% endblock %}
{% block content %}
    {% from "layout/macros.html" import query_param_list, show_raw_document %}
    <div class="panel highlight">
        <h2 class="followed">{{ product.name }}</h2>
        <div class="header-follow">
            product of type
            <a href="{{ url_for('product.metadata_type_page', name=product.metadata_type.name) }}">
                {{ product.metadata_type.name }}
            </a>
        </div>
        <div>
            <em>{{ product.definition['description'] }}</em>
        </div>

        <p class="header-stat-information">
        {% if product_summary.dataset_count == 0 %}
            <span class="badge header-badge">
                No datasets
            </span>
        {% else %}
            {%  if product_summary %}
                <i class="fa fa-globe" aria-hidden="true"></i>
                <a href="{{ url_for('overview_page', product_name=product.name) }}">
                    Extending {% if product_summary.time_earliest -%}
                        {{ product_summary.time_earliest.strftime('%B %Y') }} to
                        {{ product_summary.time_latest.strftime('%B %Y') }}
                    {%- endif %}
                </a>
            {% else %}
                <span class="badge header-badge">Product not summarised</span> (unknown statistics)<br/>
            {% endif %}
            <br/>
            <i class="fa fa-list" aria-hidden="true"></i>
            <a href="{{ url_for('search_page', product_name=product.name) }}">
                {{ '{:,d}'.format(product_summary.dataset_count) if product_summary else 'View ' }}
                datasets
            </a>
        {% endif %}
        </p>
    </div>

    <div class="panel odd overflowable">
        <h3>Location{% if location_samples | length > 1 %}s{% endif %} <span class="addendum">(from sampling)</span></h3>
        {% for location in location_samples %}
            <div class="dataset-location">
                <div class="uri-path">{{ location.common_prefix }}</div>
                <ul class="quiet">
                    {% for example_uris in location.example_uris %}
                        <li class="uri-path muted">{{ example_uris }}</li>
                    {% endfor %}
                    <span title="Only samples are shown">…</span>
                </ul>
            </div>
        {% else %}
            <p>No recorded locations</p>
        {% endfor %}
    </div>
    <div class="panel">

        <h3>Metadata</h3>

        {% if (product.fields | all_values_none) and (product_summary.fixed_metadata == {}) %}
            <em>No common values</em>
        {% else %}
            {{ query_param_list(product.fields,
                            show_nulls=false,
                            wide=true,
                            descriptions=product.metadata_type.dataset_fields,
                            fallback_dict=product_summary.fixed_metadata or {}) }}
            {% if product_summary.fixed_metadata is none %}
                <em title="Generated summaries are out of date">
                    <i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
                    Unknown fixed fields
                </em>
            {% endif %}
        {% endif %}
    </div>

    <div class="panel">
        <h3>Searchable fields</h3>
        {% for key, value in product.fields | dictsort if (value is none and key not in fixed_metadata) %}
            <div>
                {% set field = product.metadata_type.dataset_fields[key] %}
                {{ key }}

                <span class="badge">
                    {{- field.type_name -}}
                    {%- if field.indexed is true %}
                        <span title="indexed"><i class="fa fa-square" aria-hidden="true"></i></span>
                    {%- endif -%}
                </span>
                {% if field.description and ('TODO' not in field.description) %}
                    <span class="muted">{{ field.description }}</span>
                {% endif %}
            </div>
        {% endfor %}
    </div>
    <div class="panel">

        {% if product.definition.measurements %}
            <h3>Measurements</h3>

            {% for measurement in product.definition.measurements %}
                <div>

                    <span>{{ measurement.name }}</span>
                    {%- for a in measurement.aliases %}
                        / <span class="muted">{{ a }}</span>
                    {%- endfor %}

                    <span class="badge">{{ measurement.dtype }}</span>
                    <span class="muted">
                        with units {{ measurement.units }} and nodata {{ measurement.nodata }}
                        </span>
                </div>
            {% endfor %}
        {% endif %}
    </div>

    {% if product.definition.storage %}
        <div class="panel">
            {# These fields are enforced by dataset-type-schema.yaml#}
            <h3>Storage Parameters</h3>
            {{ query_param_list(product.definition.storage, show_dicts=true) }}
        </div>
    {% endif %}

    <div class="panel odd">
        <h3 class="followed">Definition</h3>
        <div class="header-follow">
            <a href="{{ url_for('product.raw_product_doc', name=product.name) }}" class="badge">
                RAW
                <i class="fa fa-file-text-o" aria-hidden="true"></i>
            </a>
            <a href="{{ url_for('stac.collection', collection=product.name) }}"
               class="badge">
                As Stac
                <i class="fa fa-file-text-o" aria-hidden="true"></i>
            </a>
        </div>
        {{ show_raw_document(metadata_doc) }}
    </div>

{% endblock %}
