package com.admicro.vertxlet.core;

import com.admicro.vertxlet.core.impl.HttpServerVerticle;
import com.admicro.vertxlet.core.impl.ServerOptions;
import com.admicro.vertxlet.util.FileUtils;
import com.admicro.vertxlet.util.XmlConverter;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.json.JsonObject;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public interface HttpServer {

    static final String LOG4J_DELEGATE_FACTORY_CLASS = "io.vertx.core.logging.Log4jLogDelegateFactory";
    static final String SLF4J_DELEGATE_FACTORY_CLASS = "io.vertx.core.logging.SLF4JLogDelegateFactory";
    static final String JUL_DELEGATE_FACTORY_CLASS = "io.vertx.core.logging.JULLogDelegateFactory";

    static final String LOGGING_PROPERTY_KEY = "vertx.logger-delegate-factory-class-name";

    static final String LOG4J = "log4j";
    static final String SLF4J = "slf4j";

    public static void startNew() throws VertxletException {
        HttpServer.startNew(HttpContext.defaultContext());
    }

    public static void startNew(HttpContext serverContext) throws VertxletException {
        try {
            final ClassLoader cl = HttpServer.class.getClassLoader();
            String xmlContent = FileUtils.readAll(cl, serverContext.configurationPath())
                    .replaceAll("\t", "").replaceAll("\n", "").replaceAll(" ", "");

            JsonObject config = XmlConverter.toJson(xmlContent, HttpContext.ROOT_TAG)
                    .getJsonObject("server");

            // apply system properties before create new Vert.x instance
            JsonObject properties;
            if ((properties = config.getJsonObject("properties")) != null) {
                String delegate = JUL_DELEGATE_FACTORY_CLASS;

                String logger = properties.getString("logger");
                if (LOG4J.equalsIgnoreCase(logger)) {
                    delegate = LOG4J_DELEGATE_FACTORY_CLASS;
                } else if (SLF4J.equalsIgnoreCase(logger)) {
                    delegate = SLF4J_DELEGATE_FACTORY_CLASS;
                }

                System.setProperty(LOGGING_PROPERTY_KEY, delegate);
            }

            Vertx vertx;

            // apply vertxlet options
            if (config.containsKey("vertx_options")) {
                vertx = Vertx.vertx(new VertxOptions(config.getJsonObject("vertx_options")));
            } else {
                vertx = Vertx.vertx();
            }

            // apply database options
            if (config.containsKey("database_options")) {
                JsonObject dbConfig = config.getJsonObject("database_options");
                vertx.sharedData().getLocalMap(HttpServerVerticle.DEFAULT_SHARE_LOCAL_MAP)
                        .put("db_options", dbConfig);
            }

            // apply server options
            ServerOptions options = (config.containsKey("server_options"))
                    ? new ServerOptions(config.getJsonObject("server_options")) : new ServerOptions();
            HttpServerVerticle server = new HttpServerVerticle(options);

            // apply deployment options
            DeploymentOptions deploymentOptions = (config.containsKey("deployment_options"))
                    ? new DeploymentOptions(config.getJsonObject("deployment_options")) : null;
            if (deploymentOptions != null) {
                vertx.deployVerticle(server, deploymentOptions);
            } else {
                vertx.deployVerticle(server);
            }
        } catch (IOException | ParserConfigurationException | SAXException | NullPointerException e) {
            throw new VertxletException(e);
        }
    }
}

@@ -252,33 +252,128 @@
 tx.core.
-DeploymentOptions
+AsyncResult;%0Aimport io.vertx.core.DeploymentOptions;%0Aimport io.vertx.core.Handler;%0Aimport io.vertx.core.Verticle
 ;%0Aimport
@@ -3205,26 +3205,16 @@
         
-HttpServer
 Verticle
@@ -3256,16 +3256,16 @@
 tions);%0A
+
 %0A       
@@ -3481,16 +3481,8 @@
 s%22))
- : null;
 %0A   
@@ -3494,13 +3494,23 @@
     
-if (d
+        : new D
 eplo
@@ -3521,32 +3521,20 @@
 tOptions
- != null) %7B%0A    
+();%0A
         
@@ -3583,18 +3583,25 @@
 tOptions
-);
+, ar -%3E %7B
 %0A       
@@ -3609,60 +3609,130 @@
     
-%7D else %7B%0A                vertx.
+    if (ar.failed()) %7B%0A                    vertx.close(v -%3E System.err.println(%22Error when 
 deploy
-V
+ v
 erticle
-(server
+, vertx closed%22)
 );%0A 
@@ -3738,25 +3738,45 @@
             
+ 
-%7D
+   %7D%0A            %7D);
 %0A        %7D c

Add handler when deploy verticle failed