package com.eactive.eai.rms.onl.loader.service;

import com.eactive.eai.common.util.UUIDGenerator;
import com.eactive.eai.rms.bat.loader.intf.InterfaceDeployService;
import com.eactive.eai.rms.bat.loader.intf.deploy.InterfaceDeploy;
import com.eactive.eai.rms.common.context.MonitoringContext;
import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.onl.loader.service.enm.ResourceType;
import com.eactive.eai.rms.onl.loader.vo.RuleFileResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

@Service
public class DeployResourceDownloadBatService extends DeployResourceDownloadService {

	@Autowired
	@Qualifier("monitoringContext")
	private MonitoringContext monitoringContext;

   @Autowired
   @Qualifier("BatInterfaceDeployService")
   private InterfaceDeployService interfaceDeployService;

	@Override
	public boolean supportsType(String serviceType) {
		return DataSourceTypeManager.BAT.equals(serviceType);
	}

	public RuleFileResource getResource(String serviceType, ResourceType resourceType, String resourceName)
			throws Exception {

		DataSourceType dt = DataSourceTypeManager.getDataSourceType(serviceType.toUpperCase());
		DataSourceContextHolder.setDataSourceType(dt);

		return getInterfaceResource(dt, resourceName);
	}

	public byte[] getResourcesZip(Map<String, String>[] paramGridRows) throws Exception {

		final String tempFilePath = monitoringContext.getStringProperty(MonitoringContext.IOMAP_DOWNLOAD_PATH);

		Path tempFileUuidDirectory = Paths
				.get(tempFilePath + File.separator + UUIDGenerator.getUUID() + File.separator);
		Files.createDirectories(tempFileUuidDirectory);

		try {

			for (Map<String, String> row : paramGridRows) {

				String resourceType = row.get("resourceType");
				String resourceName = row.get("resourceName").substring(0, row.get("resourceName").lastIndexOf("."));
				String resourceApp = row.get("resourceApp");

				RuleFileResource fileContent = getInterfaceResource(DataSourceContextHolder.getDataSourceType(),
						resourceName);

				Files.createDirectories(Paths.get(tempFileUuidDirectory + File.separator + resourceApp + File.separator
						+ resourceType + File.separator));
				Files.write(Paths.get(tempFileUuidDirectory + File.separator + resourceApp + File.separator
						+ resourceType + File.separator + fileContent.getFileName()),
						fileContent.getFileContent().getBytes());

			}

			byte[] pathToZipFile = pathToZipFile(tempFileUuidDirectory);

			return pathToZipFile;

		} finally {
			deleteDirectoryRecursively(tempFileUuidDirectory);
		}

	}

	public RuleFileResource getInterfaceResource(DataSourceType dt, String eaiSvcName) throws Exception {
      InterfaceDeploy interfaceDeploy = interfaceDeployService.selectDeploy(eaiSvcName);
      String json = mapper.writeValueAsString(interfaceDeploy);
      return new RuleFileResource(eaiSvcName + ".txt", json);
	}

}