package io.cloudslang.content.sitescope.services;

import io.cloudslang.content.httpclient.entities.HttpClientInputs;
import io.cloudslang.content.httpclient.services.HttpClientService;
import io.cloudslang.content.sitescope.constants.Inputs;
import io.cloudslang.content.sitescope.constants.SuccessMsgs;
import io.cloudslang.content.sitescope.entities.DeleteMonitorGroupInputs;
import io.cloudslang.content.sitescope.entities.DeployTemplateInputs;
import io.cloudslang.content.sitescope.entities.SiteScopeCommonInputs;
import io.cloudslang.content.sitescope.utils.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.jetbrains.annotations.NotNull;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import static io.cloudslang.content.httpclient.build.auth.AuthTypes.BASIC;
import static io.cloudslang.content.sitescope.constants.Constants.*;
import static io.cloudslang.content.sitescope.constants.Inputs.DeployTemplate.*;
import static io.cloudslang.content.sitescope.services.HttpCommons.setCommonHttpInputs;

public class DeployTemplateService {

    public @NotNull
    Map<String,String> execute(@NotNull DeployTemplateInputs deployTemplateInputs) throws Exception{
        final HttpClientInputs httpClientInputs = new HttpClientInputs();
        final SiteScopeCommonInputs commonInputs = deployTemplateInputs.getCommonInputs();

        httpClientInputs.setUrl(commonInputs.getProtocol() + "://" + commonInputs.getHost() + COLON + commonInputs.getPort() +
                SITESCOPE_TEMPLATES_API + DEPLOY_TEMPLATE_ENDPOINT);

        setCommonHttpInputs(httpClientInputs, commonInputs);

        httpClientInputs.setAuthType(BASIC);
        httpClientInputs.setUsername(commonInputs.getUsername());
        httpClientInputs.setPassword(commonInputs.getPassword());
        httpClientInputs.setMethod(POST);
        httpClientInputs.setContentType(X_WWW_FORM);
        httpClientInputs.setFormParams(populateDeployTemplateFormParams(deployTemplateInputs));
        httpClientInputs.setFormParamsAreURLEncoded(String.valueOf(true));
//        httpClientInputs.setBody("pathToTemplate:TestNG_sis_path_delimiter_Test_Template\n" +
//                "pathToTargetGroup:Template_Group");
        httpClientInputs.setResponseCharacterSet(commonInputs.getResponseCharacterSet());

        Map<String, String> httpClientOutputs = new HttpClientService().execute(httpClientInputs);

        return HttpUtils.convertToSitescopeResultsMap(httpClientOutputs, SuccessMsgs.DEPLOY_TEMPLATE);
    }

    public static String populateDeployTemplateFormParams(DeployTemplateInputs deployTemplateInputs){
        String delimiter = deployTemplateInputs.getDelimiter();
        String pathToTemplate = deployTemplateInputs.getPathToTemplate();
        String pathToTargetGroup = deployTemplateInputs.getPathToTargetGroup();
        String connectToServer = deployTemplateInputs.getConnectToServer();
        String testRemotes = deployTemplateInputs.getTestRemotes();
        String customParameters = deployTemplateInputs.getCustomParameters();

        if(!delimiter.isEmpty()){
            pathToTemplate = pathToTemplate.replace(delimiter,SITE_SCOPE_DELIMITER);
            pathToTargetGroup = pathToTargetGroup.replace(delimiter,SITE_SCOPE_DELIMITER);
        }

        Map<String, String> inputsMap = new HashMap<>();
        inputsMap.put(PATH_TO_TARGET_GROUP, pathToTargetGroup);
        inputsMap.put(CONNECT_TO_SERVER, connectToServer);
        inputsMap.put(TEST_REMOTES, testRemotes);
      //  inputsMap.put(CUSTOM_PARAMETERS, customParameters);
        inputsMap.put(PATH_TO_TEMPLATE, pathToTemplate);


        URIBuilder ub = new URIBuilder();

        for (Map.Entry<String, String> entry : inputsMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (!value.isEmpty())
                ub.addParameter(key, value);
        }

        return ub.toString();
    }
}

@@ -2135,159 +2135,8 @@
 ));%0A
-//        httpClientInputs.setBody(%22pathToTemplate:TestNG_sis_path_delimiter_Test_Template%5Cn%22 +%0A//                %22pathToTargetGroup:Template_Group%22);%0A
     
@@ -2670,16 +2670,57 @@
 mplate()
+.replace(delimiter,SITE_SCOPE_DELIMITER);
 ;%0A      
@@ -2791,16 +2791,56 @@
 tGroup()
+.replace(delimiter,SITE_SCOPE_DELIMITER)
 ;%0A      
@@ -3068,300 +3068,88 @@
     
-if(!delimiter.isEmpty())%7B%0A            pathToTemplate = pathToTemplate.replace(delimiter,SITE_SCOPE_DELIMITER);%0A            pathToTargetGroup = pathToTargetGroup.replace(delimiter,SITE_SCOPE_DELIMITER);%0A        %7D%0A%0A        Map%3CString, String%3E inputsMap = new HashMap%3C%3E();%0A        inputsMap.put(
+StringBuilder formParams = new StringBuilder();%0A        appendParams(formParams,
 PATH
@@ -3194,30 +3194,40 @@
         
-inputsMap.put(
+appendParams(formParams,
 CONNECT_
@@ -3263,30 +3263,40 @@
         
-inputsMap.put(
+appendParams(formParams,
 TEST_REM
@@ -3325,86 +3325,34 @@
     
-//  inputsMap.put(CUSTOM_PARAMETERS, customParameters);%0A        inputsMap.put(
+  appendParams(formParams,
 PATH
@@ -3382,26 +3382,24 @@
 plate);%0A
-%0A%0A
         
 URIBuild
@@ -3394,39 +3394,67 @@
     
-URIBuilder ub = new URIBuilder(
+appendParams(formParams,CUSTOM_PARAMETERS, customParameters
 );%0A%0A
@@ -3465,274 +3465,232 @@
     
-for (Map.Entry%3CString, String%3E entry : inputsMap.entrySet()) %7B
+return formParams.toString().replace(%22 %22,%22%2520%22).substring(1);
 %0A    
+%7D%0A%0A
     
-    String key = entry.getKey();%0A            String value = entry.getValue();%0A            if (!value.isEmpty())%0A                ub.addParameter(key, value);%0A        %7D%0A%0A        return ub.toString(
+public static void appendParams(StringBuilder formParams,String key, String value)%7B%0A        formParams.append(%22&%22).append(key).append(%22=%22).append(value
 );%0A 

Change mechanism for formParams creation