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

import com.eactive.eai.common.util.UUIDGenerator;
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.common.util.FileWriteUtil;
import com.eactive.eai.rms.onl.loader.dao.LoaderDao;
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.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeployResourceDownloadBapService extends DeployResourceDownloadService {

	@Autowired
	@Qualifier("loaderDao")
	private LoaderDao dao;

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

	@Autowired
	@Qualifier("loaderService")
	private TransactionLoaderService loaderService;

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

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

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

		switch (resourceType) {
		case INTERFACE:
			return getInterfaceResource(dt, resourceName);
		default:
			throw new IllegalArgumentException("Unexpected value: " + resourceType);
		}

	}

	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);
		}

	}

	private RuleFileResource getInterfaceResource(DataSourceType dt, String eaiSvcName) throws Exception {

		List<LinkedHashMap> listBJ01 = dao.selectTableData(dt, "TSEAIBJ01", "INFID", eaiSvcName);
		if (listBJ01 == null || listBJ01.size() == 0) {
			throw new Exception("fileInfo data is empty  fileName =[" + eaiSvcName + "]");
		}

		LinkedHashMap<String, List> tables = new LinkedHashMap<String, List>();
		tables.put("TSEAIBJ01", listBJ01);

		return new RuleFileResource(eaiSvcName + ".txt", FileWriteUtil.StringWrite(tables));

	}

}