/*
 * Copyright 2018 EPAM Systems
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epam.ta.reportportal.core.integration.util;

import com.epam.reportportal.extension.PluginCommand;
import com.epam.reportportal.extension.ReportPortalExtensionPoint;
import com.epam.ta.reportportal.commons.validation.BusinessRule;
import com.epam.ta.reportportal.core.plugin.PluginBox;
import com.epam.ta.reportportal.dao.IntegrationRepository;
import com.epam.ta.reportportal.entity.integration.Integration;
import com.epam.ta.reportportal.entity.integration.IntegrationParams;
import com.epam.ta.reportportal.entity.integration.IntegrationType;
import com.epam.ta.reportportal.entity.project.Project;
import com.epam.ta.reportportal.exception.ReportPortalException;
import com.epam.ta.reportportal.ws.model.ErrorType;
import com.epam.ta.reportportal.ws.model.integration.IntegrationRQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.epam.ta.reportportal.ws.model.ErrorType.BAD_REQUEST_ERROR;
import static java.util.Optional.ofNullable;

/**
 * @author <a href="mailto:pavel_bortnik@epam.com">Pavel Bortnik</a>
 */
@Service
public class BasicIntegrationServiceImpl implements IntegrationService {

	private final String TEST_CONNECTION_COMMAND = "testConnection";

	protected IntegrationRepository integrationRepository;

	protected PluginBox pluginBox;

	@Autowired
	public BasicIntegrationServiceImpl(IntegrationRepository integrationRepository, PluginBox pluginBox) {
		this.integrationRepository = integrationRepository;
		this.pluginBox = pluginBox;
	}

	@Override
	public Map<String, Object> retrieveIntegrationParams(Map<String, Object> integrationParams) {
		return integrationParams;
	}

	@Override
	public Integration createIntegration(IntegrationRQ integrationRq, IntegrationType integrationType) {
		Integration integration = new Integration();
		integration.setCreationDate(LocalDateTime.now());
		Map<String, Object> integrationParams = retrieveIntegrationParams(integrationRq.getIntegrationParams());
		integration.setParams(new IntegrationParams(integrationParams));
		integration.setType(integrationType);
		integration.setEnabled(integrationRq.getEnabled());
		integration.setName(integrationRq.getName());
		return integration;
	}

	@Override
	public Integration updateIntegration(Integration integration, IntegrationRQ integrationRQ) {
		Map<String, Object> integrationParams = retrieveIntegrationParams(integrationRQ.getIntegrationParams());
		IntegrationParams params = getIntegrationParams(integration, integrationParams);
		integration.setParams(params);
		integration.setEnabled(integrationRQ.getEnabled());
		integration.setName(integrationRQ.getName());
		return integration;
	}

	@Override
	public boolean validateIntegration(Integration integration) {
		List<Integration> global = integrationRepository.findAllGlobalByType(integration.getType());
		BusinessRule.expect(global, List::isEmpty).verify(ErrorType.UNABLE_INTERACT_WITH_INTEGRATION,
				"Integration with type " + integration.getType().getName() + " is already exists"
		);
		return true;
	}

	@Override
	public boolean validateIntegration(Integration integration, Project project) {
		List<Integration> integrations = integrationRepository.findAllByProjectIdAndType(project.getId(), integration.getType());
		BusinessRule.expect(integrations, List::isEmpty).verify(ErrorType.UNABLE_INTERACT_WITH_INTEGRATION,
				"Integration with type " + integration.getType().getName() + " is already exists for project " + project.getName()
		);
		return true;
	}

	@Override
	public boolean checkConnection(Integration integration) {
		ReportPortalExtensionPoint pluginInstance = pluginBox.getInstance(integration.getType().getName(), ReportPortalExtensionPoint.class)
				.orElseThrow(() -> new ReportPortalException(BAD_REQUEST_ERROR,
						"Plugin for {} isn't installed",
						integration.getType().getName()
				));

		PluginCommand commandToExecute = Optional.ofNullable(pluginInstance.getCommandToExecute(TEST_CONNECTION_COMMAND))
				.orElseThrow(() -> new ReportPortalException(BAD_REQUEST_ERROR,
						"Command {} is not found in plugin {}.",
						TEST_CONNECTION_COMMAND,
						integration.getType().getName()
				));

		return (Boolean) commandToExecute.executeCommand(integration, integration.getParams().getParams());
	}

	public static IntegrationParams getIntegrationParams(Integration integration, Map<String, Object> retrievedParams) {
		return ofNullable(integration.getParams()).map(params -> new IntegrationParams(ofNullable(params.getParams()).map(paramsMap -> {
			paramsMap.putAll(retrievedParams);
			return paramsMap;
		}).orElse(retrievedParams))).orElseGet(() -> new IntegrationParams(retrievedParams));
	}
}

@@ -1682,53 +1682,8 @@
 ROR;
-%0Aimport static java.util.Optional.ofNullable;
 %0A%0A/*
@@ -5082,25 +5082,11 @@
 %7B%0A%09%09
-return ofNullable
+if 
 (int
@@ -5109,29 +5109,21 @@
 ms()
-).map(params -%3E new I
+ != null && i
 nteg
@@ -5132,170 +5132,155 @@
 tion
+.get
 Params(
-ofNullable(params.getParams()).map(paramsMap -%3E %7B%0A%09%09%09paramsMap.putAll(retrievedParams);%0A%09%09%09return paramsMap;%0A%09%09%7D).orElse(retrievedParams))).orElseGet(() -%3E
+).getParams() != null) %7B%0A%09%09%09integration.getParams().getParams().putAll(retrievedParams);%0A%09%09%09return integration.getParams();%0A%09%09%7D%0A%09%09return
  new
@@ -5314,16 +5314,15 @@
 dParams)
-)
 ;%0A%09%7D%0A%7D%0A

fix retrieving integration params