require([
  'dojo/topic',
  'CesiumAdaptor/CesiumViewer',
  'helpers/NodeCache',
  'style/NodeStyler',
  'style/DefaultStyle',
  'style/NodeLevelStyle',
  'gui/Legend',
  'helpers/Timeout',
  'state/State',
  'dojo/domReady!'
], function (topic, CesiumViewer, NodeCache, NodeStyler, DefaultStyle, NodeLevelStyle, Legend, Timeout, State) {

  Cesium.BingMapsApi.defaultKey = "AmdoJEIiP8BcA__QO9SjBAgv4h73-uiH4lGinmzwyfjK-Fjs8t7GbmfpFjmyj1cl";
  let osm = Cesium.createOpenStreetMapImageryProvider({
    url: 'https://a.tile.openstreetmap.org/'
  });

  CesiumViewer.createViewer('cesiumContainer', {
    baseLayerPicker: false,
    imageryProvider: osm,
    fullscreenButton: false,
    scene3DOnly: true,
    timeline: false,
    animation: false,
    selectionIndicator: false,
    infoBox: false
  });

  let legend = new Legend("wrapper", "i3sNodeLevelLegend");
  legend.setRight("20px");
  legend.setBottom("60px");
  legend.setTitle("Node Level");
  legend.addEntry(Cesium.Color.ORANGE.toBytes().slice(0, -1), "1");
  legend.addEntry(Cesium.Color.BLUEVIOLET.toBytes().slice(0, -1), "2");
  legend.addEntry(Cesium.Color.CYAN.toBytes().slice(0, -1), "3");
  legend.addEntry(Cesium.Color.GREENYELLOW.toBytes().slice(0, -1), "4");
  legend.addEntry(Cesium.Color.DEEPPINK.toBytes().slice(0, -1), "5");
  legend.addEntry(Cesium.Color.BLUE.toBytes().slice(0, -1), "6");
  legend.addEntry(Cesium.Color.RED.toBytes().slice(0, -1), "7");
  legend.setVisible(false);
  legend.render();

  let state = new State();

  $("#host").val("81.169.187.7");
  $("#port").val("9000");
  $("#endpoint").val("service/v1");

  $("#request").click(function (event) {
    event.preventDefault();
    state.makeRequest();

  });

  $("#stylesList").change(function () {

    let selection = this.value;
    switch (selection) {
      case "Default":
        NodeStyler.setStyle(new DefaultStyle());
        NodeCache.updateStyle();
        legend.hide();
        break;
      case "Node Level":
        NodeStyler.setStyle(new NodeLevelStyle());
        NodeCache.updateStyle();
        legend.show();
        break;
      default:
        NodeStyler.setStyle(new DefaultStyle());
    }

  });

  $("#setBoundingBox").click(function (event) {
    state.setBoundingBox();
  });

  $("#toggleFreezeScene").change(function () {
    state.toggleFreezeScene();
  });

  let timeout = new Timeout(1000,
    function () {
      state.makeRequest();
    }
  );

  topic.subscribe("CameraChanged", function (event) {
    timeout.trigger();
  });

  $("#setPlace").click(function (event) {
    state.changeState("ManualRequest");
    state.makeRequest();
  });

});