{% include "header.html" %}
{% if params %}
  {% set urlq = url + '?' + params + '&' %}
  {% else %}
  {% set urlq = url + '?' %}
{% endif %}

<nav aria-label="breadcrumb">
  <ol class="breadcrumb bg-light">
    {% for crumb in crumbs %}
      {% if not loop.last %}
    <li class="breadcrumb-item"><a href="{{ crumb.url }}/">{{ crumb.part }}</a></li>
      {% else %}<li class="breadcrumb-item active" aria-current="page">{{ crumb.part }}</li>
      {% endif %}
    {% endfor %}

    <li class="ml-auto json-link"><a target="_blank" href="{{ urlq }}f=json">JSON</a></li>
  </ol>
</nav>

<h1>{{ response.title or response.id }}</h1>

<div class="row">
  <div class="col-sm">
    <ul>
      {% for key in ['title', 'description', 'id', 'uri', 'crs', 'wellKnownScaleSet', 'orderedAxes'] %}
        {% if response[key]  %}
          <li><strong>{{ key }}:</strong> {{ response[key] }}</li>
        {% endif %}
      {% endfor %}

      <li><strong>Nb TileMatrices:</strong> {{ response.tileMatrices|count }}</li>
      <li><strong>Min level:</strong> {{ response.tileMatrices[0].id }}</li>
      <li><strong>Max level:</strong> {{ response.tileMatrices[-1].id }}</li>
      <li><strong>Origin:</strong> {{ response.tileMatrices[0].pointOfOrigin }}</li>
      <li><strong>Tile size:</strong> {{ [response.tileMatrices[0].tileWidth, response.tileMatrices[0].tileHeight] }}</li>

    </ul>
  </div>
  <div class="col-sm">
    <div id="map" class="rounded" style="width: 100%; height: 400px">
      Loading...
    </div>
  </div>
</div>

<script>
  $(function() {
    var map = L.map('map').setView([0, 0], 1);
    map.addLayer(new L.TileLayer(
      'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
        maxZoom: 18,
        attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
      }
    ));

    var bbox = {{ response.bbox or [-180,-90,180,90] }};

    const bboxPolygon = (bounds) => {
      return {
        'type': 'Feature',
        'geometry': {
            'type': 'Polygon',
            'coordinates': [[
                [bounds[0], bounds[1]],
                [bounds[2], bounds[1]],
                [bounds[2], bounds[3]],
                [bounds[0], bounds[3]],
                [bounds[0], bounds[1]]
            ]]
        },
        'properties': {}
      }
    }

    if (bbox[0] > bbox[2]) {
      geo = {
        "type": "FeatureCollection",
        "features": [
          bboxPolygon([-180, bbox[1], bbox[2], bbox[3]]),
          bboxPolygon([bbox[0], bbox[1], 180, bbox[3]]),
        ]
      }
    } else {
      geo = {
        "type": "FeatureCollection",
        "features": [bboxPolygon(bbox)]
      }
    }

    var aoi = L.geoJSON(geo).addTo(map);
    map.fitBounds(aoi.getBounds());
  });
</script>
{% include "footer.html" %}
