package nl.jpoint.vertx.mod.deploy.handler;

import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import nl.jpoint.vertx.mod.deploy.request.DeployState;
import nl.jpoint.vertx.mod.deploy.service.AwsService;
import nl.jpoint.vertx.mod.deploy.service.DeployApplicationService;
import nl.jpoint.vertx.mod.deploy.util.ApplicationDeployState;
import nl.jpoint.vertx.mod.deploy.util.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RestDeployStatusHandler implements Handler<RoutingContext> {
    private final Logger LOG = LoggerFactory.getLogger(RestDeployStatusHandler.class);

    private final AwsService deployAwsService;
    private final DeployApplicationService deployApplicationService;

    public RestDeployStatusHandler(AwsService deployAwsService, DeployApplicationService deployApplicationService) {
        this.deployAwsService = deployAwsService;
        this.deployApplicationService = deployApplicationService;
    }

    @Override
    public void handle(final RoutingContext context) {
        DeployState state = deployAwsService != null ? deployAwsService.getDeployStatus(context.request().params().get("id")) : DeployState.SUCCESS;

        if (!deployApplicationService.getDeployedApplicationsFailed().isEmpty()) {
            LOG.error("Some services failed to start, failing build");
            state = DeployState.FAILED;
            if (deployAwsService != null) {
                deployAwsService.failAllRunningRequests();
            }
        }

        switch (state != null ? state : DeployState.CONTINUE) {
            case SUCCESS:
                respondOk(context.request());
                deployApplicationService.cleanup();
                break;
            case UNKNOWN:
            case FAILED:
                respondFailed(context.request());
                deployApplicationService.cleanup();
                break;
            default:
                respondContinue(context.request(), state);
        }

    }

    private void respondOk(HttpServerRequest request) {
        request.response().setStatusCode(HttpResponseStatus.OK.code());
        request.response().end(createStatusObject().encode());

    }

    private void respondContinue(HttpServerRequest request, DeployState state) {
        request.response().setStatusCode(HttpResponseStatus.ACCEPTED.code());
        request.response().setStatusMessage("Deploy in state : " + state.name());
        request.response().end();
    }

    private void respondFailed(HttpServerRequest request) {
        request.response().setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
        request.response().end(createStatusObject().encode());
    }

    private JsonObject createStatusObject() {
        JsonObject result = new JsonObject();
        result.put(ApplicationDeployState.OK.name(), HttpUtils.toArray(deployApplicationService.getDeployedApplicationsSuccess()));
        result.put(ApplicationDeployState.ERROR.name(), HttpUtils.toArray(deployApplicationService.getDeployedApplicationsFailed()));
        return result;
    }
}
@@ -1653,25 +1653,24 @@
       %7D%0A
-%0A
         
 switch (
@@ -1665,16 +1665,34 @@
     
-switch (
+DeployState deployState = 
 stat
@@ -1731,16 +1731,45 @@
 CONTINUE
+;%0A        switch (deployState
 ) %7B%0A    
@@ -2159,17 +2159,23 @@
 uest(), 
-s
+deployS
 tate);%0A 

Fix npe in deployState