package nl.jpoint.vertx.mod.cluster.handler.servicebus;

import nl.jpoint.vertx.mod.cluster.aws.AwsState;
import nl.jpoint.vertx.mod.cluster.request.*;
import nl.jpoint.vertx.mod.cluster.service.AwsService;
import nl.jpoint.vertx.mod.cluster.service.DeployArtifactService;
import nl.jpoint.vertx.mod.cluster.service.DeployConfigService;
import nl.jpoint.vertx.mod.cluster.service.DeployModuleService;
import nl.jpoint.vertx.mod.cluster.util.LogConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vertx.java.core.Handler;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.core.json.JsonObject;

public class DeployHandler implements Handler<Message<JsonObject>> {

    private static final Logger LOG = LoggerFactory.getLogger(DeployHandler.class);

    private final AwsService awsService;
    private final DeployModuleService deployModuleService;
    private final DeployArtifactService deployArtifactService;
    private final DeployConfigService deployConfigService;

    public DeployHandler(AwsService awsService, DeployModuleService deployModuleService, DeployArtifactService deployArtifactService, DeployConfigService deployConfigService) {
        this.awsService = awsService;
        this.deployModuleService = deployModuleService;
        this.deployArtifactService = deployArtifactService;
        this.deployConfigService = deployConfigService;
    }

    @Override
    public void handle(Message<JsonObject> message) {
        JsonObject body = message.body();
        String deployId = body.getString("id");
        AwsState state = AwsState.valueOf(body.getString("state"));

        if (!body.getBoolean("success")) {
            awsService.failBuild(deployId);
            return;
        }

        LOG.info("[{} - {}]: Handle internal deploy request with state {}", LogConstants.DEPLOY_REQUEST, deployId, state);
        switch (state) {
            case STANDBY:
            case NOTREGISTERED:
            case OUTOFSERVICE:
                this.deployArtifacts(deployId);
                break;
            case INSERVICE:
                this.registerInstance(deployId);
                break;
            default:
                // do nothing
        }
    }

    private void registerInstance(String deployId) {
        awsService.updateAndGetRequest(DeployState.SUCCESS, deployId);
    }

    private void deployArtifacts(String deployId) {
        DeployRequest deployRequest = awsService.updateAndGetRequest(DeployState.DEPLOYING_CONFIGS, deployId);

        boolean deployOk = false;

        if (deployRequest.withRestart()) {
            deployModuleService.stopContainer(deployId);
        }

        if (deployRequest.getConfigs() != null) {
            for (DeployConfigRequest configRequests : deployRequest.getConfigs()) {
                deployOk = deployConfigService.deploy(configRequests);
                if (!deployOk) {
                    awsService.failBuild(deployId);
                    return;
                }
            }
        }

        deployRequest = awsService.updateAndGetRequest(DeployState.DEPLOYING_ARTIFACTS, deployId);

        if (deployRequest.getArtifacts() != null) {
            for (DeployArtifactRequest artifactRequest : deployRequest.getArtifacts()) {
                deployOk = deployArtifactService.deploy(artifactRequest);

                if (!deployOk) {
                    awsService.failBuild(deployId);
                    return;
                }
            }
        }

        deployRequest = awsService.updateAndGetRequest(DeployState.DEPLOYING_MODULES, deployId);

        if (deployOk && deployRequest.getModules() != null) {
            for (DeployModuleRequest moduleRequest : deployRequest.getModules()) {
                deployOk = deployModuleService.deploy(moduleRequest);

                if (!deployOk) {
                    awsService.failBuild(deployId);
                    return;
                }
            }
        }
        awsService.updateAndGetRequest(DeployState.WAITING_FOR_REGISTER, deployId);
        awsService.registerInstance(deployId);
    }


}

@@ -2703,36 +2703,77 @@
 onfigs() != null
+ && !deployRequest.getConfigs().isEmpty()
 ) %7B%0A
-
             for 
@@ -3207,32 +3207,75 @@
 ifacts() != null
+ && !deployRequest.getArtifacts().isEmpty()
 ) %7B%0A            
@@ -3672,32 +3672,32 @@
 ES, deployId);%0A%0A
-
         if (depl
@@ -3738,16 +3738,57 @@
  != null
+ && !deployRequest.getModules().isEmpty()
 ) %7B%0A    

Check if collecions are not empty