/**
 * 
 */
package org.springblade.etl.engine.seatunnel;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.springblade.core.tool.exception.ServiceException;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.etl.connector.base.SeatunnelConnector;
import org.springblade.etl.connector.transform.PlatomixRowStackTransform;
import org.springblade.etl.engine.flink.dto.SubmitJobReq;
import org.springblade.etl.entity.Job;
import org.springblade.etl.entity.JobLink;
import org.springblade.etl.entity.JobTask;
import org.springblade.etl.enums.ConnectorType;
import org.springblade.etl.enums.InstanceType;
import org.springblade.etl.enums.JdbcType;
import org.springblade.etl.enums.PluginType;
import org.springblade.etl.properties.EtlProperties;
import org.springblade.etl.service.JobLinkService;
import org.springblade.etl.service.JobService;
import org.springblade.etl.service.JobTaskService;
import org.springblade.etl.util.EtlJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.MutableGraph;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wookvn
 *
 */
@Slf4j
@Service
public class SeatunnelService {
	
	@Autowired
	private JobService jobService;
	
	@Autowired
	private JobTaskService jobTaskService;
	
	@Autowired
	private JobLinkService jobLinkService;
	
	@Autowired
	private EtlProperties etlConfig;
	
	@Autowired
	private org.springblade.etl.engine.JobService flinkJobService;
	
	public SubmitJobReq buildJobRequest(Job job, List<JobTask> taskList, List<JobLink> taskLinkList) {
		Integer jobId = Optional.<Integer>ofNullable(job.getId()).orElse(0);
		log.info("======>>开始构造flink请求体｜jobid={}<<<<==========", jobId);
		//1.先构造seatunnel config文件对应的json
		SeatunnelJobConfig jobConfig = new SeatunnelJobConfig();
		if(CollectionUtil.isEmpty(taskList) || CollectionUtil.isEmpty(taskLinkList)) {
			throw new ServiceException("请检查该任务的节点和连线是否正确|jobId=" + jobId);
		}
		// build env
		SeatunnelJobEnv jobEnv = new SeatunnelJobEnv(job.getJobName(), InstanceType.BATCH.getMode());
		if(taskList.stream().anyMatch(item -> item.getConnectorType().isStreaming())){ //mysqlcdc/kafka 需要开启checkpoint
			jobEnv.setCheckpointInterval(5000L);
			jobEnv.setJobMode(InstanceType.STREAMING.getMode());
		}
		jobConfig.setEnv(jobEnv);
		
		//构造seatunel_job请求必须参数
		Set<ConnectorType> plugins = new HashSet<>();
		Set<JdbcType> jdbcs = new HashSet<>();
		
		// build source, sink, transform
		MutableGraph<ObjectNode> graph = GraphBuilder.directed().allowsSelfLoops(true).build();
		Map<String, ObjectNode> stepMap = new HashMap<>();
		//构造节点
		for(JobTask jobTask : taskList) {
			SeatunnelConnector<?> seatunnelConnector = SpringUtil.getBean(jobTask.getConnectorType().getClazz());
			ObjectNode nodeConfig = null;
			if(seatunnelConnector instanceof PlatomixRowStackTransform) {
				nodeConfig = ((PlatomixRowStackTransform)seatunnelConnector).buildConfig(jobTask, taskList);
			}else {
				nodeConfig = seatunnelConnector.buildConfig(jobTask);
			}
			graph.addNode(nodeConfig);
			stepMap.put(jobTask.getTaskCode(), nodeConfig);
			
			plugins.add(jobTask.getConnectorType());
			if(nodeConfig.get(SeaTunnelConstant.JDBC_TYPE) != null) {
				jdbcs.add(JdbcType.valueOf(nodeConfig.get(SeaTunnelConstant.JDBC_TYPE).asText()));
			}
		}
		//构造连线
		taskLinkList.stream().forEach(item -> {
			String fromCode = item.getFromCode(),
				   toCode   = item.getToCode();
			graph.putEdge(stepMap.get(fromCode), stepMap.get(toCode));
		});
		//设置result_table_name和source_table_name
		graph.edges().forEach(edge -> {
			ObjectNode sourceConfig = edge.source(),
					   targetConfig = edge.target();
			//表名需要去掉一些关键字符[比如：_]
			String tableName = (SeaTunnelConstant.SEATUNNEL_TABLE_NAME_PREFIX + sourceConfig.get(SeaTunnelConstant.PLUGIN_NAME).asText() + "_" + sourceConfig.get(SeaTunnelConstant.NODE_ID).asInt()).replaceAll("\\-", "_").toLowerCase();
			sourceConfig.put(SeaTunnelConstant.RESULT_TABLE_NAME, tableName);
			//source_table_name支持数组形式
			targetConfig.withArray(SeaTunnelConstant.SOURCE_TABLE_NAME).add(tableName);
		});
		//构造结果Config
		graph.nodes().forEach(node -> {
			PluginType pluginType = PluginType.valueOf(node.get(SeaTunnelConstant.PLUGIN_TYPE).asText());
			switch(pluginType) {
				case SOURCE:{
					jobConfig.getSource().add(node);
					break;
				}
				case SINK:{
					jobConfig.getSink().add(node);
					break;
				}
				case TRANSFORM:{
					jobConfig.getTransform().add(node);
					break;
				}
				default:
			}
		});
		//删除无效的键值
		graph.nodes().forEach(node -> node.remove(Arrays.asList(SeaTunnelConstant.PLUGIN_TYPE, SeaTunnelConstant.JDBC_TYPE, SeaTunnelConstant.NODE_ID)));
		String configJson = EtlJsonUtil.toPrettyJson(jobConfig);
		log.info("====>>seatunnelConfig: \n{}", configJson);
		SubmitJobReq submitJobReq = new SubmitJobReq();
		submitJobReq.setJobName(job.getJobName());
		submitJobReq.setJobConfigJson(configJson);
		submitJobReq.setPlugins(plugins);
		submitJobReq.setJdbcs(jdbcs);
		if(etlConfig.getFlinkWebInterfaceUrls().size() == 1) {
			submitJobReq.setFlinkInterfaceUrl(etlConfig.getFlinkWebInterfaceUrls().get(0));
		}else {
			//获取负载最低的
			String lighterLoadWebIntefaceUrl = flinkJobService.lighterLoad(etlConfig.getFlinkWebInterfaceUrls());
			submitJobReq.setFlinkInterfaceUrl(lighterLoadWebIntefaceUrl);
		}
		if(StrUtil.isBlank(submitJobReq.getFlinkInterfaceUrl())) {
			throw new ServiceException("未找到有效的flink节点");
		}
		return submitJobReq;
	}
	
	/**
	 * 通过JobId构造任务请求
	 * @param jobId
	 * @return
	 */
	public SubmitJobReq buildJobRequest(Integer jobId) {
		Job job = jobService.getById(jobId);
		List<JobTask> taskList = jobTaskService.listByJobId(jobId);
		List<JobLink> taskLinkList = jobLinkService.listByJobId(jobId);
		return buildJobRequest(job, taskList, taskLinkList);
	}
}
