/*
 * Copyright 2020 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.events.handler;

import com.epam.ta.reportportal.core.integration.migration.JiraEmailSecretMigrationService;
import com.epam.ta.reportportal.core.integration.migration.LdapSecretMigrationService;
import com.epam.ta.reportportal.core.integration.migration.RallySecretMigrationService;
import com.epam.ta.reportportal.core.integration.migration.SaucelabsSecretMigrationService;
import com.epam.ta.reportportal.exception.ReportPortalException;
import com.epam.ta.reportportal.filesystem.DataStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author <a href="mailto:ihar_kahadouski@epam.com">Ihar Kahadouski</a>
 */
@Component
public class IntegrationSecretsMigrationHandler {

	private static final Logger LOGGER = LoggerFactory.getLogger(IntegrationSecretsMigrationHandler.class);

	@Value("${rp.integration.salt.path:keystore}")
	private String integrationSaltPath;

	@Value("${rp.integration.salt.migration:migration}")
	private String migrationFile;

	private final DataStore dataStore;

	private final JiraEmailSecretMigrationService jiraEmailSecretMigrationService;

	private final RallySecretMigrationService rallySecretMigrationService;

	private final SaucelabsSecretMigrationService saucelabsSecretMigrationService;

	private final LdapSecretMigrationService ldapSecretMigrationService;

	@Autowired
	public IntegrationSecretsMigrationHandler(DataStore dataStore, JiraEmailSecretMigrationService jiraEmailSecretMigrationService,
			RallySecretMigrationService rallySecretMigrationService, SaucelabsSecretMigrationService saucelabsSecretMigrationService,
			LdapSecretMigrationService ldapSecretMigrationService) {
		this.dataStore = dataStore;
		this.jiraEmailSecretMigrationService = jiraEmailSecretMigrationService;
		this.rallySecretMigrationService = rallySecretMigrationService;
		this.saucelabsSecretMigrationService = saucelabsSecretMigrationService;
		this.ldapSecretMigrationService = ldapSecretMigrationService;
	}

	@EventListener
	public void migrate(ApplicationReadyEvent event) throws IOException {
		final String migrationFilePath = integrationSaltPath + File.separator + migrationFile;
		try (InputStream load = dataStore.load(migrationFilePath)) {
			ExecutorService executor = Executors.newFixedThreadPool(4);
			executor.execute(jiraEmailSecretMigrationService::migrate);
			executor.execute(rallySecretMigrationService::migrate);
			executor.execute(saucelabsSecretMigrationService::migrate);
			executor.execute(ldapSecretMigrationService::migrate);
			executor.shutdown();
			dataStore.delete(migrationFilePath);
		} catch (ReportPortalException ex) {
			LOGGER.info("Secrets migration is not needed");
		}
	}
}

@@ -1557,92 +1557,8 @@
 eam;
-%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;
 %0A%0A/*
@@ -3203,88 +3203,8 @@
 %0A%09%09%09
-ExecutorService executor = Executors.newFixedThreadPool(4);%0A%09%09%09executor.execute(
 jira
@@ -3234,40 +3234,23 @@
 vice
-::
+.
 migrate
+(
 );%0A%09%09%09
-executor.execute(
 rall
@@ -3276,40 +3276,23 @@
 vice
-::
+.
 migrate
+(
 );%0A%09%09%09
-executor.execute(
 sauc
@@ -3322,40 +3322,23 @@
 vice
-::
+.
 migrate
+(
 );%0A%09%09%09
-executor.execute(
 ldap
@@ -3363,40 +3363,16 @@
 vice
-::
+.
 migrate
-);%0A%09%09%09executor.shutdown
 ();%0A

remove thread pool to avoid conflicts while secret migration