package io.georocket.http;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;

import io.georocket.GeoRocket;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;

/**
 * An HTTP endpoint for general requests
 * @author Michel Kraemer
 */
public class GeneralEndpoint implements Endpoint {
  private final String version;

  /**
   * Create the endpoint
   */
  public GeneralEndpoint() {
    version = getVersion();
  }
  
  /**
   * @return the tool's version string
   */
  private static String getVersion() {
    URL u = GeoRocket.class.getResource("version.dat");
    String version;
    try {
      version = IOUtils.toString(u, StandardCharsets.UTF_8);
    } catch (IOException e) {
      throw new RuntimeException("Could not read version information", e);
    }
    return version;
  }

  @Override
  public String getMountPoint() {
    return "/";
  }

  @Override
  public Router createRouter(Vertx vertx) {
    Router router = Router.router(vertx);
    router.get("/").handler(this::onInfo);
    router.head("/").handler(this::onPing);
    router.options("/").handler(this::onOptions);
    return router;
  }
  
  /**
   * Send information about GeoRocket to the client
   * @param context the context for handling HTTP requests
   */
  protected void onInfo(RoutingContext context) {
    JsonObject o = new JsonObject()
        .put("name", "GeoRocket")
        .put("version", version)
        .put("tagline", "It's not rocket science!");
    context.response()
      .setStatusCode(200)
      .putHeader("Content-Type", "application/json")
      .end(o.encodePrettily());
  }
  
  /**
   * Send an empty response with a status code of 200 to the client
   * @param context the context for handling HTTP requests
   */
  protected void onPing(RoutingContext context) {
    context.response()
      .setStatusCode(200)
      .end();
  }

  /**
   * Handle the HTTP options request
   * @param context the context for handling HTTP requests
   */
  protected void onOptions(RoutingContext context) {
    context.response()
      .putHeader("Allow", "GET,HEAD")
      .setStatusCode(200)
      .end();
  }
}

@@ -1275,24 +1275,424 @@
  router;%0A  %7D
+%0A%0A  /**%0A   * Create a JSON object that can be returned by %7B@link #onInfo(RoutingContext)%7D%0A   * @param context the current routing context%0A   * @return the JSON object%0A   */%0A  protected JsonObject createInfo(RoutingContext context) %7B%0A    JsonObject o = new JsonObject()%0A      .put(%22name%22, %22GeoRocket%22)%0A      .put(%22version%22, version)%0A      .put(%22tagline%22, %22It's not rocket science!%22);%0A    return o;%0A  %7D
 %0A  %0A  /**%0A  
@@ -1879,142 +1879,26 @@
 o = 
-new JsonObject()%0A        .put(%22name%22, %22GeoRocket%22)%0A        .put(%22version%22, version)%0A        .put(%22tagline%22, %22It's not rocket science!%22
+createInfo(context
 );%0A 

Allow server info to be overridden