/**
   * It helps to create each layer in a open layer script
   *
   * @param actualLayer
   * @param layerCount
   * @param visible Boolean indicates if the layer should be visible
   * @return
   */
  private String layerHelper(Layer actualLayer, int layerCount, boolean visible) {
    String layersScript = "";
    // If they layer is a jsonp (dynamic vector) layer, we do not add it to the map
    if (!actualLayer.isJsonp()) {
      if (actualLayer.isTiled()) {
        layersScript += "\tlayer" + layerCount + " = new ol.layer.Tile({\n";
        layersScript += "\t\t source: new ol.source.TileWMS({\n";
      } else {
        layersScript += "\tlayer" + layerCount + " = new ol.layer.Image({\n";
        layersScript += "\t\t source: new ol.source.ImageWMS({\n";
        layersScript += "\t\t extent: [\n" + actualLayer.getBbox().toString() + "],";
      }
      layersScript +=
          "\t\t url: '"
              + actualLayer.getServer()
              + "',\n"
              //				+ "\t\t crossOrigin: 'null',\n"
              + "\t\t params: {LAYERS: '"
              + actualLayer.getName()
              + "'";

      if (actualLayer.isncWMS()) {
        if (actualLayer.getMaxColor() != -1 && actualLayer.getMinColor() != -1) {
          layersScript +=
              ", numcolorbands:250,  colorscalerange: '"
                  + actualLayer.getMinColor()
                  + ","
                  + actualLayer.getMaxColor()
                  + "'";
        }
      }
      layersScript += ", STYLES: '" + actualLayer.getStyle() + "'";

      layersScript += ", SRS: _map_projection";

      layersScript += "}\n\t\t\t})\n";
      layersScript += "\t\t});\n";

      // In this case the layer has some CQL that we need to add in its configuration
      if (!actualLayer.getCql().equals("")) {
        layersScript +=
            "\tlayer"
                + layerCount
                + ".getSource().getParams().CQL_FILTER = \""
                + actualLayer.getCql()
                + "\";\n";
      }

      if (!visible) {
        layersScript +=
            "\tlayer" + layerCount + ".setVisible(false);\n"; // we make the layer not visible.
      }
      layersScript += "\tmap.addLayer(layer" + layerCount + ");\n";
      //			layersScript += "\tlayer"+layerCount+".getSource().on('change'))\n";
    } else {
      layersScript +=
          "\t owgis.vector.manager.requestJSONLayer("
              + actualLayer.getLayerDetails()
              + ","
              + layerCount
              + ","
              + visible
              + ");\n";
    }
    return layersScript;
  }
  //	private String generateURLhelper(Layer actualLayer, int ajaxCallNumber, int layerNumber) {
  private String generateURLhelper(Layer actualLayer, int layerNumber) {

    String URLscript = "";

    URLscript += "\t\tif(typeof(layer" + layerNumber + ") !== 'undefined'){\n";
    URLscript += "\t\t\tif(layer" + layerNumber + ".getVisible()){\n";
    URLscript +=
        "\t\t\t\tvar url"
            + layerNumber
            + " = basepath+\"/redirect?server="
            + actualLayer.getServer()
            + "&";

    URLscript += "LAYERS=" + actualLayer.getFeatureInfoLayer() + "&";
    URLscript +=
        "STYLES=&"
            + "WIDTH=\"+ map.getSize()[0] +\"&"
            + "HEIGHT=\"+ map.getSize()[1] +\"&"
            + "SRS=\"+ _map_projection+ \"&"
            + "FORMAT="
            + actualLayer.getFormat()
            + "&"
            + "VERSION=1.1.1&"
            + "REQUEST=GetFeatureInfo&"
            + "EXCEPTIONS=application/vnd.ogc.se_xml&"
            + "x=\"+ Math.floor(evt.pixel[0]) +\"&"
            + "y=\"+ Math.floor(evt.pixel[1]) +\"&"
            + "BBOX=\"+ currBBOX +\"&"
            + "SERVICE=WMS&";

    if (!actualLayer.getCql().equals("")) {
      URLscript += "CQL_FILTER=" + actualLayer.getCql() + "&";
    }

    // In this case we also need the time information
    if (actualLayer.isncWMS()) {
      // The two variables: elevation and startDate have to match
      // javascript variable names.
      //			URLscript += "ELEVATION=\"+layerDetails.zaxis.values[owgis.ncwms.zaxis.globcounter]+\"&"
      URLscript +=
          "\"+owgis.ncwms.zaxis.addElevationText()+\""
              + "TIME=\"+owgis.ncwms.calendars.getCurrentlySelectedDate(\"yy-mm-dd\")+\"&"
              + "BOTHTIMES=\"+getUserSelectedTimeFrame()+\"&"
              + "INFO_FORMAT=text/xml&"
              + "NETCDF=true&";
    } else {
      URLscript += "INFO_FORMAT=text/html&" + "NETCDF=false&";
    }

    URLscript += "QUERY_LAYERS=" + actualLayer.getFeatureInfoLayer() + "&";
    URLscript += "FEATURE_COUNT=50\";\n";
    URLscript +=
        "\t\t\t\t var asynchronous"
            + layerNumber
            + " = new Asynchronous();\n"
            + "\t\t\t\t asynchronous"
            + layerNumber
            + ".complete = AsyncPunctualData;\n"
            //				+ "\t\t\t alert(url" + layerNumber + ");\n"
            + "\t\t\t\t asynchronous"
            + layerNumber
            + ".call(url"
            + layerNumber
            + ");\n"
            + "\t\t\t}\n"
            + "\t\t}\n";
    return URLscript;
  }