/*
 * Copyright 2019 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.impl;

import com.epam.reportportal.extension.PluginCommand;
import com.epam.reportportal.extension.ReportPortalExtensionPoint;
import com.epam.ta.reportportal.commons.ReportPortalUser;
import com.epam.ta.reportportal.core.integration.ExecuteIntegrationHandler;
import com.epam.ta.reportportal.core.integration.util.IntegrationService;
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.exception.ReportPortalException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Optional;

import static com.epam.ta.reportportal.ws.model.ErrorType.BAD_REQUEST_ERROR;
import static com.epam.ta.reportportal.ws.model.ErrorType.INTEGRATION_NOT_FOUND;

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

	private final Map<String, IntegrationService> integrationServiceMapping;

	private final IntegrationRepository integrationRepository;

	private final PluginBox pluginBox;

	private final IntegrationService basicIntegrationService;

	public ExecuteIntegrationHandlerImpl(IntegrationRepository integrationRepository, PluginBox pluginBox,
			@Qualifier("integrationServiceMapping") Map<String, IntegrationService> integrationServiceMapping,
			@Qualifier("basicIntegrationServiceImpl") IntegrationService basicIntegrationService) {
		this.integrationServiceMapping = integrationServiceMapping;
		this.integrationRepository = integrationRepository;
		this.pluginBox = pluginBox;
		this.basicIntegrationService = basicIntegrationService;
	}

	@Override
	public Object executeCommand(ReportPortalUser.ProjectDetails projectDetails, Long integrationId, String command,
			Map<String, ?> executionParams) {
		Integration integration = integrationRepository.findByIdAndProjectId(integrationId, projectDetails.getProjectId())
				.orElse(integrationRepository.findGlobalById(integrationId)
						.orElseThrow(() -> new ReportPortalException(INTEGRATION_NOT_FOUND, integrationId)));

		IntegrationService integrationService = integrationServiceMapping.getOrDefault(integration.getType().getName(),
				basicIntegrationService
		);
		integrationService.decryptParams(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(command))
				.orElseThrow(() -> new ReportPortalException(BAD_REQUEST_ERROR,
						"Command {} is not found in plugin {}.",
						command,
						integration.getType().getName()
				));

		return commandToExecute.executeCommand(integration, executionParams);
	}
}

@@ -647,62 +647,8 @@
 l;%0A%0A
-import com.epam.reportportal.extension.PluginCommand;%0A
 impo
@@ -710,16 +710,16 @@
 nPoint;%0A
+
 import c
@@ -1297,35 +1297,8 @@
 Map;
-%0Aimport java.util.Optional;
 %0A%0Aim
@@ -1452,16 +1452,61 @@
 T_FOUND;
+%0Aimport static java.util.Optional.ofNullable;
 %0A%0A/**%0A *
@@ -2696,17 +2696,26 @@
 %09.orElse
-(
+Get(() -%3E 
 integrat
@@ -3343,50 +3343,15 @@
 %0A%0A%09%09
-PluginCommand commandToExecute = Optional.
+return 
 ofNu
@@ -3401,16 +3401,75 @@
 ommand))
+.map(it -%3E it.executeCommand(integration, executionParams))
 %0A%09%09%09%09.or
@@ -3633,88 +3633,15 @@
 e()%0A
+
 %09%09%09%09));
-%0A%0A%09%09return commandToExecute.executeCommand(integration, executionParams);
 %0A%09%7D%0A

fix finding integration for execute command