package org.mmog2048.verticles;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.handler.sockjs.BridgeOptions;
import io.vertx.ext.web.handler.sockjs.PermittedOptions;
import io.vertx.ext.web.handler.sockjs.SockJSHandler;
import io.vertx.redis.RedisClient;
import io.vertx.redis.RedisOptions;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.mmog2048.dao.RedisDAO;
import org.mmog2048.utils.RedisUtils;

import java.util.Date;

public class WebServer extends AbstractVerticle {
  private static final Logger LOG = LoggerFactory.getLogger(WebServer.class);

  @Override
  public void start(Future<Void> future) {
    try {
      JsonObject config = context.config();
      RedisOptions redisOptions = RedisUtils.createRedisOptions(config.getJsonObject("redis"));
      RedisDAO redisDAO = new RedisDAO(RedisClient.create(vertx, redisOptions));

      // todo What do we want to do with this guy
//      redisDAO

      EventBus eb = vertx.eventBus();
      vertx.setPeriodic(
          1000, event -> {
            String now = DateFormatUtils.ISO_DATETIME_FORMAT.format(new Date());
            System.out.println(now);

            eb.publish("org.mmog2048:game-state",
                new JsonObject().put("serverTime", now));

            eb.publish("org.mmog2048:status-message", "Hello Cleveland " + now);
          }
      );

      SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
      BridgeOptions options = new BridgeOptions();
      PermittedOptions permitted = new PermittedOptions(); /* allow everything, we don't care for the demo */
      options.addOutboundPermitted(permitted);
      options.addInboundPermitted(permitted);
      sockJSHandler.bridge(options);

      Router router = Router.router(vertx);
      StaticHandler assetHandler = StaticHandler
          .create()
          .setDirectoryListing(true);

      router.route("/eventbus/*").handler(sockJSHandler);
      router.get("/*").handler(assetHandler);

      Integer httpPort = 8080;

      vertx
          .createHttpServer()
          .requestHandler(router::accept)
          .listen(httpPort);

      LOG.info("Listening on " + httpPort);
      future.complete();
    } catch (Exception ioe) {
      LOG.error(ioe.getMessage(), ioe);
      future.fail(ioe);
    }
  }
}
@@ -564,24 +564,75 @@
 disOptions;%0A
+import org.apache.commons.lang3.RandomStringUtils;%0A
 import org.a
@@ -1637,17 +1637,54 @@
 llo 
-Cleveland
+%22 + RandomStringUtils.randomAlphabetic(10) + %22
  %22 +

I'm bored