class WorkspaceVariableImpl {

    @NotNull
    public static Map<String, Map<String, String>> updateWorkspaceVariables(@NotNull TerraformWorkspaceVariableInputs terraformWorkspaceVariableInputs) throws Exception {
        String workspaceVariableName;
        String workspaceVariableValue;
        String sensitiveWorkspaceVariableValue;
        String workspaceVariableCategory;
        String workspaceVariableId;
        String hcl = null;
        String sensitive = null;
        Map<String, Map<String, String>> updateWorkspaceVariableMap = new HashMap<>();
        Map<String, String> listWorkspaceVariablesResult = listWorkspaceVariables(terraformWorkspaceVariableInputs);
        JSONParser parser = new JSONParser();
        final HttpClientInputs httpClientInputs = new HttpClientInputs();
        final TerraformCommonInputs commonInputs = terraformWorkspaceVariableInputs.getCommonInputs();
        httpClientInputs.setUrl(createWorkspaceVariableUrl(terraformWorkspaceVariableInputs));
        setCommonHttpInputs(httpClientInputs, commonInputs);
        httpClientInputs.setAuthType(ANONYMOUS);
        httpClientInputs.setMethod(PATCH);
        httpClientInputs.setContentType(APPLICATION_VND_API_JSON);
        httpClientInputs.setHeaders(getAuthHeaders(commonInputs.getAuthToken()));
        if (!terraformWorkspaceVariableInputs.getWorkspaceVariableJson().isEmpty() & !terraformWorkspaceVariableInputs.getSensitiveWorkspaceVariableJson().isEmpty()) {

            JSONArray updateWorkspaceVariableJsonArray = (JSONArray) parser.parse(terraformWorkspaceVariableInputs.getWorkspaceVariableJson());
            JSONArray updateSensitiveWorkspaceVariableJsonArray = (JSONArray) parser.parse(terraformWorkspaceVariableInputs.getSensitiveWorkspaceVariableJson());
            JSONObject updateWorkspaceVariableJson;
            JSONObject updateSensitiveWorkspaceVariableJson;
            for (int i = 0; i < updateWorkspaceVariableJsonArray.size(); i++) {
                updateWorkspaceVariableJson = (JSONObject) updateWorkspaceVariableJsonArray.get(i);
                workspaceVariableName = (String) updateWorkspaceVariableJson.get("propertyName");
                workspaceVariableValue = (String) updateWorkspaceVariableJson.get("propertyValue");
                workspaceVariableCategory = (String) updateWorkspaceVariableJson.get("Category");
                if (updateWorkspaceVariableJson.containsKey("HCL")) {
                    hcl = Boolean.toString((boolean) updateWorkspaceVariableJson.get("HCL"));
                }
                if (updateWorkspaceVariableJson.containsKey("sensitive")) {
                    sensitive = Boolean.toString((boolean) updateWorkspaceVariableJson.get("sensitive"));
                }
                JSONObject workspaceVariableJsonObj = (JSONObject) parser.parse(listWorkspaceVariablesResult.get("returnResult"));
                JSONArray workspaceVariableJsonArray = (JSONArray) workspaceVariableJsonObj.get("data");
                for (int j = 0; j < workspaceVariableJsonArray.size(); j++) {
                    final String workspaceVariableCatagory = JsonPath.read(workspaceVariableJsonArray.get(j), "attributes.category");
                    final String workspaceVariableKey = JsonPath.read(workspaceVariableJsonArray.get(j), "attributes.key");
                    if (workspaceVariableName.equals(workspaceVariableKey)&& workspaceVariableCategory.equals(workspaceVariableCatagory)) {
                        workspaceVariableId = JsonPath.read(workspaceVariableJsonArray.get(j), "id");
                        terraformWorkspaceVariableInputs = TerraformWorkspaceVariableInputs.builder()
                                .workspaceVariableName(workspaceVariableName)
                                .workspaceVariableValue(workspaceVariableValue)
                                .hcl(hcl)
                                .sensitive(sensitive)
                                .workspaceVariableCategory(workspaceVariableCategory)
                                .workspaceVariableId(workspaceVariableId)
                                .workspaceId(terraformWorkspaceVariableInputs.getWorkspaceId())
                                .build();
                        httpClientInputs.setBody(updateWorkspaceVariableRequestBody(terraformWorkspaceVariableInputs));
                        httpClientInputs.setUrl(updateWorkspaceVariableUrl(terraformWorkspaceVariableInputs));
                        updateWorkspaceVariableMap.put(workspaceVariableName, new HttpClientService().execute(httpClientInputs));
                    }
                }
            }
            for (int i = 0; i < updateSensitiveWorkspaceVariableJsonArray.size(); i++) {
                updateSensitiveWorkspaceVariableJson = (JSONObject) updateSensitiveWorkspaceVariableJsonArray.get(i);
                workspaceVariableName = (String) updateSensitiveWorkspaceVariableJson.get("propertyName");
                sensitiveWorkspaceVariableValue = (String) updateSensitiveWorkspaceVariableJson.get("propertyValue");
                workspaceVariableCategory = (String) updateSensitiveWorkspaceVariableJson.get("Category");
                JSONObject sensitiveWorkspaceVariableJsonObj = (JSONObject) parser.parse(listWorkspaceVariablesResult.get("returnResult"));
                JSONArray sensitiveWorkspaceVariableJsonArray = (JSONArray) sensitiveWorkspaceVariableJsonObj.get("data");
                for (int j = 0; j < sensitiveWorkspaceVariableJsonArray.size(); j++) {
                    final String workspaceVariableCatagory = JsonPath.read(sensitiveWorkspaceVariableJsonArray.get(j), "attributes.category");
                    final String sensitiveWorkspaceVariableKey = JsonPath.read(sensitiveWorkspaceVariableJsonArray.get(j), "attributes.key");
                    if (workspaceVariableName.equals(sensitiveWorkspaceVariableKey)&&workspaceVariableCategory.equals(workspaceVariableCatagory)) {
                        workspaceVariableId = JsonPath.read(sensitiveWorkspaceVariableJsonArray.get(j), "id");
                        terraformWorkspaceVariableInputs = TerraformWorkspaceVariableInputs.builder()
                                .workspaceVariableName(workspaceVariableName)
                                .workspaceVariableValue(EMPTY)
                                .sensitiveWorkspaceVariableValue(sensitiveWorkspaceVariableValue)
                                .workspaceVariableCategory(workspaceVariableCategory)
                                .workspaceVariableId(workspaceVariableId)
                                .workspaceId(terraformWorkspaceVariableInputs.getWorkspaceId())
                                .build();
                        httpClientInputs.setBody(updateWorkspaceVariableRequestBody(terraformWorkspaceVariableInputs));
                        httpClientInputs.setUrl(updateWorkspaceVariableUrl(terraformWorkspaceVariableInputs));
                        updateWorkspaceVariableMap.put(workspaceVariableName, new HttpClientService().execute(httpClientInputs));
                    }
                }
            }
            return updateWorkspaceVariableMap;
        } else if (!terraformWorkspaceVariableInputs.getWorkspaceVariableJson().isEmpty()) {
            JSONArray updateWorkspaceVariableJsonArray = (JSONArray) parser.parse(terraformWorkspaceVariableInputs.getWorkspaceVariableJson());
            JSONObject updateWorkspaceVariableJson;
            for (int i = 0; i < updateWorkspaceVariableJsonArray.size(); i++) {
                updateWorkspaceVariableJson = (JSONObject) updateWorkspaceVariableJsonArray.get(i);
                workspaceVariableName = (String) updateWorkspaceVariableJson.get("propertyName");
                workspaceVariableValue = (String) updateWorkspaceVariableJson.get("propertyValue");
                workspaceVariableCategory = (String) updateWorkspaceVariableJson.get("Category");
                if (updateWorkspaceVariableJson.containsKey("HCL")) {
                    hcl = Boolean.toString((boolean) updateWorkspaceVariableJson.get("HCL"));
                }
                if (updateWorkspaceVariableJson.containsKey("sensitive")) {
                    sensitive = Boolean.toString((boolean) updateWorkspaceVariableJson.get("sensitive"));
                }
                JSONObject workspaceVariableJsonObj = (JSONObject) parser.parse(listWorkspaceVariablesResult.get("returnResult"));
                JSONArray workspaceVariableJsonArray = (JSONArray) workspaceVariableJsonObj.get("data");
                for (int j = 0; j < workspaceVariableJsonArray.size(); j++) {
                    final String workspaceVariableCatagory = JsonPath.read(workspaceVariableJsonArray.get(j), "attributes.category");
                    final String workspaceVariableKey = JsonPath.read(workspaceVariableJsonArray.get(j), "attributes.key");
                    if (workspaceVariableName.equals(workspaceVariableKey)&& workspaceVariableCategory.equals(workspaceVariableCatagory)) {
                        workspaceVariableId = JsonPath.read(workspaceVariableJsonArray.get(j), "id");
                        terraformWorkspaceVariableInputs = TerraformWorkspaceVariableInputs.builder()
                                .workspaceVariableName(workspaceVariableName)
                                .workspaceVariableValue(workspaceVariableValue)
                                .hcl(hcl)
                                .sensitive(sensitive)
                                .workspaceVariableCategory(workspaceVariableCategory)
                                .workspaceVariableId(workspaceVariableId)
                                .workspaceId(terraformWorkspaceVariableInputs.getWorkspaceId())
                                .build();
                        httpClientInputs.setBody(updateWorkspaceVariableRequestBody(terraformWorkspaceVariableInputs));
                        httpClientInputs.setUrl(updateWorkspaceVariableUrl(terraformWorkspaceVariableInputs));
                        updateWorkspaceVariableMap.put(workspaceVariableName, new HttpClientService().execute(httpClientInputs));
                    }
                }
            }
            return updateWorkspaceVariableMap;
        } else {
            JSONArray updateSensitiveWorkspaceVariableJsonArray = (JSONArray) parser.parse(terraformWorkspaceVariableInputs.getSensitiveWorkspaceVariableJson());
            JSONObject updateSensitiveWorkspaceVariableJson;
            for (int i = 0; i < updateSensitiveWorkspaceVariableJsonArray.size(); i++) {
                updateSensitiveWorkspaceVariableJson = (JSONObject) updateSensitiveWorkspaceVariableJsonArray.get(i);
                workspaceVariableName = (String) updateSensitiveWorkspaceVariableJson.get("propertyName");
                sensitiveWorkspaceVariableValue = (String) updateSensitiveWorkspaceVariableJson.get("propertyValue");
                workspaceVariableCategory = (String) updateSensitiveWorkspaceVariableJson.get("Category");
                JSONObject sensitiveWorkspaceVariableJsonObj = (JSONObject) parser.parse(listWorkspaceVariablesResult.get("returnResult"));
                JSONArray sensitiveWorkspaceVariableJsonArray = (JSONArray) sensitiveWorkspaceVariableJsonObj.get("data");
                for (int j = 0; j < sensitiveWorkspaceVariableJsonArray.size(); j++) {
                    final String workspaceVariableCatagory = JsonPath.read(sensitiveWorkspaceVariableJsonArray.get(j), "attributes.category");
                    final String sensitiveWorkspaceVariableKey = JsonPath.read(sensitiveWorkspaceVariableJsonArray.get(j), "attributes.key");
                    if (workspaceVariableName.equals(sensitiveWorkspaceVariableKey)&&workspaceVariableCategory.equals(workspaceVariableCatagory)) {
                        workspaceVariableId = JsonPath.read(sensitiveWorkspaceVariableJsonArray.get(j), "id");
                        terraformWorkspaceVariableInputs = TerraformWorkspaceVariableInputs.builder()
                                .workspaceVariableName(workspaceVariableName)
                                .workspaceVariableValue(EMPTY)
                                .sensitiveWorkspaceVariableValue(sensitiveWorkspaceVariableValue)
                                .workspaceVariableCategory(workspaceVariableCategory)
                                .workspaceVariableId(workspaceVariableId)
                                .workspaceId(terraformWorkspaceVariableInputs.getWorkspaceId())
                                .build();
                        httpClientInputs.setBody(updateWorkspaceVariableRequestBody(terraformWorkspaceVariableInputs));
                        httpClientInputs.setUrl(updateWorkspaceVariableUrl(terraformWorkspaceVariableInputs));
                        updateWorkspaceVariableMap.put(workspaceVariableName, new HttpClientService().execute(httpClientInputs));
                    }
                }
            }

            return updateWorkspaceVariableMap;
        }


    }

}