package io.tiler.demo;

import org.simondean.vertx.async.Async;
import org.simondean.vertx.async.AsyncResultHandlerWrapper;
import org.vertx.java.core.Future;
import org.vertx.java.core.json.JsonArray;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.core.logging.Logger;
import org.vertx.java.platform.Verticle;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

public class MainVerticle extends Verticle {
  private Logger logger;
  private Map<String, String> env;

  public void start(final Future<Void> startedResult) {
    logger = container.logger();
    env = container.env();
    JsonObject config = container.config();

    Async.series()
      .task(handler -> container.deployVerticle("io.tiler.ServerVerticle", config.getObject("server"), 1, AsyncResultHandlerWrapper.wrap(handler)))
      .task(handler -> container.deployModule("io.tiler~tiler-collector-example~0.1.1", config.getObject("example"), 1, AsyncResultHandlerWrapper.wrap(handler)))
      .run(result -> {
        if (result.failed()) {
          startedResult.setFailure(result.cause());
          return;
        }

        logger.info("MainVerticle started");
        startedResult.setResult(null);
      });
  }

  private void mergeConfigWithEnvironmentVariables(JsonObject config) {
    mergeConfigWithRedisEnvironmentVariables(config);
  }

  private void mergeConfigWithRedisEnvironmentVariables(JsonObject config) {
    JsonObject server = config.getObject("server");

    if (server == null) {
      logger.warn("'server' is missing from the config");
      return;
    }

    JsonObject redis = server.getObject("redis");

    if (redis == null) {
      logger.warn("'redis' is missing from the config");
      return;
    }

    String redisUrlString = getEnvironmentVariable("REDISTOGO_URL");

    if (redisUrlString != null) {
      logger.info("Using Redis To Go");
      
      try {
        URL redisURL = new URL(redisUrlString);
        redis.putString("host", redisURL.getHost());
        redis.putNumber("port", redisURL.getPort());
      } catch (MalformedURLException e) {
        logger.error("Error parsing Redis URL", e);
      }
    }
  }

  private String getEnvironmentVariable(String name) {
    String value = env.get(name);

    if (value == null) {
      logger.warn("Environment variable '" + name + "' has not been set");
    }

    return value;
  }
}

@@ -156,51 +156,8 @@
 re;%0A
-import org.vertx.java.core.json.JsonArray;%0A
 impo
@@ -468,16 +468,45 @@
 ng%3E env;
+%0A  private JsonObject config;
 %0A%0A  publ
@@ -619,27 +619,16 @@
 ();%0A    
-JsonObject 
 config =
@@ -645,24 +645,68 @@
 .config();%0A%0A
+    mergeConfigWithEnvironmentVariables();%0A%0A
     Async.se
@@ -1307,33 +1307,16 @@
 riables(
-JsonObject config
 ) %7B%0A    
@@ -1312,32 +1312,38 @@
 es() %7B%0A    merge
+Server
 ConfigWithRedisE
@@ -1328,37 +1328,32 @@
 ServerConfigWith
-Redis
 EnvironmentVaria
@@ -1357,22 +1357,16 @@
 riables(
-config
 );%0A  %7D%0A%0A
@@ -1377,32 +1377,38 @@
 ivate void merge
+Server
 ConfigWithRedisE
@@ -1401,21 +1401,16 @@
 nfigWith
-Redis
 Environm
@@ -1422,33 +1422,16 @@
 riables(
-JsonObject config
 ) %7B%0A    
@@ -1580,24 +1580,358 @@
 urn;%0A    %7D%0A%0A
+    String portString = getEnvironmentVariable(%22PORT%22);%0A%0A    if (portString != null) %7B%0A      logger.info(%22Setting server port to %22 + portString);%0A%0A      try %7B%0A        server.putNumber(%22port%22, Integer.parseInt(portString));%0A      %7D catch (NumberFormatException e) %7B%0A        logger.error(%22Error parsing server port%22, e);%0A      %7D%0A    %7D%0A%0A
     JsonObje
@@ -2199,35 +2199,60 @@
 fo(%22
-Using Redis To Go%22);%0A      
+Setting config to Redis server %22 + redisUrlString);%0A
 %0A   

Set server port based on PORT environment variable