define([
  "dojo/_base/declare",
  "dojo/request",
  "helpers/UrlBuilder",
  "CesiumAdaptor/CesiumViewer",
  "helpers/Layers"
], function (declare, request, UrlBuilder, CesiumViewer, Layers) {

  return declare(null, {

    constructor: function () {
      this._layers = null;

    },

    getLayers: function () {

      let url = new UrlBuilder(location.protocol.slice(0, -1), $("#host").val())
        .port($("#port").val())
        .path($("#endpoint").val())
        .param("service", "3DPS")
        .param("acceptversions", "1.0")
        .param("request", "GetCapabilities")
        .build();

      request(url, { handleAs: "text" }).then(document => {

        this._layers = new Layers(document);

        $("#layersList").empty();

        $.each(this._layers.getLayers(), function (i, layer) {
          $("#layersList").append($("<option>", {
            value: layer.id,
            text: layer.name
          }));
        });

      });


    },

    selectLayer: function (layer) {
      // let selection = listElement.value;
      let layerCenter = this._layers.getLayerCenter(layer);
      let viewer = CesiumViewer.getViewer();
      let center = Cesium.Cartesian3.fromDegrees(layerCenter.lon, layerCenter.lat, 20000.0);
      viewer.camera.setView({
        destination: center
      });
    }

  });

});