package cn.axzo.workflow.core.engine.behavior;

import cn.axzo.workflow.core.engine.event.ExtTaskInstCreateEvent;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.MapExceptionEntry;
import org.flowable.bpmn.model.ServiceTask;
import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.impl.bpmn.behavior.ServiceTaskDelegateExpressionActivityBehavior;
import org.flowable.engine.impl.bpmn.parser.FieldDeclaration;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.impl.util.TaskHelper;
import org.flowable.task.service.TaskService;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;

import java.util.List;

import static cn.axzo.workflow.common.enums.BpmnProcessInstanceResultEnum.APPROVED;

/**
 * 自定义的服务任务活动行为处理器
 * <p>
 * 主要用来创建审批日志
 *
 * @author wangli
 * @since 13/03/2024 14:17
 */
@Slf4j
public class CustomServiceTaskDelegateExpressionActivityBehavior extends ServiceTaskDelegateExpressionActivityBehavior {

    protected final ServiceTask serviceTask;
    private TaskEntity task;

    public CustomServiceTaskDelegateExpressionActivityBehavior(String serviceTaskId, Expression expression,
                                                               Expression skipExpression,
                                                               List<FieldDeclaration> fieldDeclarations,
                                                               List<MapExceptionEntry> mapExceptions,
                                                               boolean triggerable, ServiceTask serviceTask) {
        super(serviceTaskId, expression, skipExpression, fieldDeclarations, mapExceptions, triggerable);
        this.serviceTask = serviceTask;
    }

    @Override
    public void execute(DelegateExecution execution) {
        CommandContext commandContext = CommandContextUtil.getCommandContext();
        ProcessEngineConfigurationImpl processEngineConfiguration =
                CommandContextUtil.getProcessEngineConfiguration(commandContext);
        TaskService taskService = processEngineConfiguration.getTaskServiceConfiguration().getTaskService();
        task = taskService.createTask();
        task.setExecutionId(execution.getId());
        task.setTaskDefinitionKey(serviceTask.getId());
        task.setPropagatedStageInstanceId(execution.getPropagatedStageInstanceId());
        task.setName(serviceTask.getName());
        TaskHelper.insertTask(task, (ExecutionEntity) execution, false, false);

        // 添加 taskInst 扩展表数据
        FlowableEventDispatcher eventDispatcher = processEngineConfiguration.getEventDispatcher();
        eventDispatcher.dispatchEvent(new ExtTaskInstCreateEvent(execution.getProcessInstanceId(),
                        execution.getCurrentActivityId(), task.getId(), APPROVED),
                processEngineConfiguration.getEngineCfgKey());
        super.execute(execution);
    }

//    @Override
//    public void leave(DelegateExecution execution) {
//        CommandContext commandContext = CommandContextUtil.getCommandContext();
//        ProcessEngineConfigurationImpl processEngineConfiguration =
//                CommandContextUtil.getProcessEngineConfiguration(commandContext);
//
//        FlowableEventDispatcher eventDispatcher = processEngineConfiguration.getEventDispatcher();
//        eventDispatcher.dispatchEvent(new ExtTaskInstUpdateEvent(execution.getProcessInstanceId(),
//                        execution.getCurrentActivityId(), task.getId(), APPROVED),
//                processEngineConfiguration.getEngineCfgKey());
//        super.leave(execution);
//    }
}
