package org.springblade.flow.engine.custom.cmd.jump;

import java.io.Serializable;
import java.util.Map;

import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.delegate.DelegateExecution;

/**
 * 会签加签CMD
 */
public class AddMulitiInstanceExecutionCmd implements Command<Void>, Serializable {
	protected final String NUMBER_OF_INSTANCES = "nrOfInstances";
	protected final String NUMBER_OF_ACTIVE_INSTANCES = "nrOfActiveInstances";
	protected final String NUMBER_OF_COMPLETED_INSTANCES = "nrOfCompletedInstances";
	protected String collectionElementIndexVariable = "loopCounter";
	private String parentExecutionId;
	private String activityId;
	private Map<String, Object> executionVariables;

	/**
	 *
	 * @param parentExecutionId  父执行实例id
	 * @param activityId 目标节点id
	 * @param executionVariables 变量
	 */
	public AddMulitiInstanceExecutionCmd(String parentExecutionId, String activityId,
                                         Map<String, Object> executionVariables) {
		this.parentExecutionId = parentExecutionId;
		this.activityId = activityId;
		this.executionVariables = executionVariables;
	}

	@Override
	public Void execute(CommandContext commandContext) {
//		ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();
//		HistoryManager historyManager = CommandContextUtil.getHistoryManager();
//		// 获取执行实例管理器
//
//		ExecutionEntity secondExecution = executionEntityManager.findById(parentExecutionId);
//		String processDefinitionId = secondExecution.getProcessDefinitionId();
//		if (secondExecution == null) {
//			throw new RuntimeException("找不到二级实例数据");
//		}
//		// 通过二级执行实例创建三级执行实例
//		ExecutionEntity childExecution = executionEntityManager.createChildExecution(secondExecution);
//
//		childExecution.setCurrentFlowElement(secondExecution.getCurrentFlowElement());
//		// 获取当前实例中的数据，因为我们要获取当前的节点，并判断是否是多实例任务节点
//		BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(processDefinitionId);
//		Activity miActivityElement = (Activity) bpmnModel
//				.getFlowElement(secondExecution.getCurrentFlowElement().getId());
//		MultiInstanceLoopCharacteristics loopCharacteristics = miActivityElement.getLoopCharacteristics();
//		if (loopCharacteristics == null) {
//			throw new RuntimeException("没有找到MultiInstanceLoopCharacteristics");
//		}
//		if (!(miActivityElement.getBehavior() instanceof MultiInstanceActivityBehavior)) {
//			throw new RuntimeException("这个节点不是多实例节点");
//		}
//
//		Integer currentNumberOfInstance = (Integer) secondExecution.getVariable(NUMBER_OF_INSTANCES);
//		secondExecution.setVariableLocal(NUMBER_OF_INSTANCES, currentNumberOfInstance + 1);
//		if (executionVariables != null) {
//			childExecution.setVariables(executionVariables);
//		}
//		historyManager.recordActivityStart(childExecution);
//		Object behavior = miActivityElement.getBehavior();
//		ParallelMultiInstanceBehavior p = (ParallelMultiInstanceBehavior) behavior;
//		p.getInnerActivityBehavior().execute(childExecution);
		return null;
	}

	protected Integer getLoopVariable(DelegateExecution execution, String variableName) {
		Object value = execution.getVariableLocal(variableName);
		DelegateExecution parent = execution.getParent();
		while (value == null && parent != null) {
			value = parent.getVariableLocal(variableName);
			parent = parent.getParent();
		}
		return (Integer) (value != null ? value : 0);
	}
}
