package cn.axzo.workflow.core.converter.json;

import cn.axzo.workflow.common.enums.ApprovalMethodEnum;
import cn.axzo.workflow.common.enums.BpmnFlowNodeMode;
import cn.axzo.workflow.common.enums.BpmnFlowNodeType;
import cn.axzo.workflow.common.model.request.bpmn.BpmnButtonConf;
import cn.axzo.workflow.common.model.request.bpmn.BpmnJsonNode;
import cn.axzo.workflow.common.model.request.bpmn.BpmnJsonNodeProperty;
import com.alibaba.fastjson.JSON;
import org.flowable.bpmn.model.ExtensionAttribute;
import org.flowable.bpmn.model.ExtensionElement;
import org.flowable.bpmn.model.FlowableListener;
import org.flowable.bpmn.model.MultiInstanceLoopCharacteristics;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.delegate.BaseExecutionListener;
import org.flowable.engine.delegate.TaskListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static cn.axzo.workflow.common.constant.BpmnConstants.AND_SIGN_EXPRESSION;
import static cn.axzo.workflow.common.constant.BpmnConstants.BPM_ALLOW_SKIP_USER_TASK;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_APPROVAL_METHOD;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_APPROVER_EMPTY_HANDLE_TYPE;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_APPROVER_MODE_TYPE;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_APPROVER_SCOPE;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_APPROVER_SPECIFY;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_BUTTON;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_BUTTON_TYPE_CARBON_COPY;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_BUTTON_TYPE_CURRENT;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_BUTTON_TYPE_HISTORY;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_BUTTON_TYPE_INITIATOR;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_FIELD_PERMISSION;
import static cn.axzo.workflow.common.constant.BpmnConstants.ELEMENT_ATTRIBUTE_DESC;
import static cn.axzo.workflow.common.constant.BpmnConstants.ELEMENT_ATTRIBUTE_VALUE;
import static cn.axzo.workflow.common.constant.BpmnConstants.INTERNAL_TASK_RELATION_ASSIGNEE_LIST_INFO;
import static cn.axzo.workflow.common.constant.BpmnConstants.OR_SIGN_EXPRESSION;
import static cn.axzo.workflow.common.enums.ApproverEmptyHandleTypeEnum.autoPassed;
import static cn.axzo.workflow.common.enums.BpmnFlowNodeType.NODE_STARTER;
import static cn.axzo.workflow.core.common.utils.BpmnJsonConverterUtil.buildMetaButton;
import static org.flowable.bpmn.model.ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION;

/**
 * 用户任务节点
 *
 * @author wangli
 * @since 2023/10/13 17:01
 */
public class UserTaskJsonConverter extends AbstractBpmnJsonConverter<UserTask> {

    @Override
    public UserTask convertJsonToElement(BpmnJsonNode node, Process process) {
        UserTask userTask = new UserTask();

        userTask.setId(node.getId());
        userTask.setName(node.getName());

        // 设置审批人为空时,允许自动通过
        setAutoPassed(node, userTask);
        // 设置会签或签
        setMultiInstance(node, userTask);
        // 设置全局任务监听器
        setTaskListeners(userTask);
        // 设置全局执行监听器
        if (!Objects.equals(NODE_STARTER, node.getType())) {
            setExecutionListeners(userTask);
        }

        //以下是保存配置信息, 引擎完全不会 care 这里的信息
        // "设置审批人"
        setApprovalExtensionElement(node, userTask);
        // "权限设置"
        setFieldExtensionElement(node, userTask);
        // "高级设置"
        setButtonExtensionElement(node, userTask);

        return userTask;
    }

    private static void setAutoPassed(BpmnJsonNode node, UserTask userTask) {
        if (Objects.nonNull(node.getProperty()) &&
                Objects.equals(node.getProperty().getApproverEmptyHandleType(), autoPassed)) {
            userTask.setSkipExpression("${" + BPM_ALLOW_SKIP_USER_TASK + "}");
        }
    }

    private static void setMultiInstance(BpmnJsonNode node, UserTask userTask) {
        if (Objects.isNull(node.getProperty())) {
            return;
        }
        MultiInstanceLoopCharacteristics multiInstanceLoopCharacteristics =
                new MultiInstanceLoopCharacteristics();
        // 审批人集合参数
        multiInstanceLoopCharacteristics.setInputDataItem(INTERNAL_TASK_RELATION_ASSIGNEE_LIST_INFO + userTask.getId());
        // 迭代集合
        multiInstanceLoopCharacteristics.setElementVariable("assigneeName");
        // 并行
        multiInstanceLoopCharacteristics.setSequential(false);
        userTask.setAssignee("${assigneeName}");
        // 设置多实例属性
        userTask.setLoopCharacteristics(multiInstanceLoopCharacteristics);

        // 设置会签或签
        if (BpmnFlowNodeMode.OR.equals(node.getProperty().getMultiMode())) {
            multiInstanceLoopCharacteristics.setCompletionCondition(OR_SIGN_EXPRESSION);
        } else if (BpmnFlowNodeMode.AND.equals(node.getProperty().getMultiMode())) {
            multiInstanceLoopCharacteristics.setCompletionCondition(AND_SIGN_EXPRESSION);
        }
    }

    private static void setButtonExtensionElement(BpmnJsonNode node, UserTask userTask) {
        if (Objects.isNull(node.getProperty())) {
            return;
        }
        BpmnButtonConf buttonConf = node.getProperty().getButtonPermission();
        ExtensionElement buttonConfigElement = new ExtensionElement();
        buttonConfigElement.setName(CONFIG_BUTTON);

        if (Objects.nonNull(buttonConf)) {
            buildMetaButton(buttonConf.getInitiator(), CONFIG_BUTTON_TYPE_INITIATOR, buttonConfigElement);
            buildMetaButton(buttonConf.getCurrent(), CONFIG_BUTTON_TYPE_CURRENT, buttonConfigElement);
            buildMetaButton(buttonConf.getHistory(), CONFIG_BUTTON_TYPE_HISTORY, buttonConfigElement);
            buildMetaButton(buttonConf.getCarbonCopy(), CONFIG_BUTTON_TYPE_CARBON_COPY, buttonConfigElement);
        }

        userTask.addExtensionElement(buttonConfigElement);
    }

    private static void setFieldExtensionElement(BpmnJsonNode node, UserTask userTask) {
        if (Objects.isNull(node.getProperty())) {
            return;
        }
        ExtensionElement fieldElement = new ExtensionElement();
        fieldElement.setName(CONFIG_FIELD_PERMISSION);
        fieldElement.setElementText(node.getProperty().getFieldPermission());
        userTask.addExtensionElement(fieldElement);
    }

    private static void setApprovalExtensionElement(BpmnJsonNode node, UserTask userTask) {
        BpmnJsonNodeProperty property = node.getProperty();
        if (Objects.isNull(property)) {
            return;
        }

        // 审批方式
        ExtensionElement approvalMethodElement = new ExtensionElement();
        approvalMethodElement.setName(CONFIG_APPROVAL_METHOD);

        ExtensionAttribute approvalMethodValueAttribute = new ExtensionAttribute();
        approvalMethodValueAttribute.setName(ELEMENT_ATTRIBUTE_VALUE);
        approvalMethodValueAttribute.setValue(property.getApprovalMethod().getType());
        approvalMethodElement.addAttribute(approvalMethodValueAttribute);

        ExtensionAttribute approvalMethodDescAttribute = new ExtensionAttribute();
        approvalMethodDescAttribute.setName(ELEMENT_ATTRIBUTE_DESC);
        approvalMethodDescAttribute.setValue("审批方式");
        approvalMethodElement.addAttribute(approvalMethodDescAttribute);
        userTask.addExtensionElement(approvalMethodElement);

        if (BpmnFlowNodeType.NODE_BUSINESS.equals(node.getType()) && ApprovalMethodEnum.nobody.equals(property.getApprovalMethod())) {
            // 业务节点有一种不设置审批人的选项, 除"审批方式"外都是空配置, 所以在此直接中断后端的扩展属性配置
            return;
        }
        // 审批人所在范围
        ExtensionElement approverScopeElement = new ExtensionElement();
        approverScopeElement.setName(CONFIG_APPROVER_SCOPE);

        ExtensionAttribute approverScopeValueAttribute = new ExtensionAttribute();
        approverScopeValueAttribute.setName(ELEMENT_ATTRIBUTE_VALUE);
        approverScopeValueAttribute.setValue(property.getApproverScope().getType());
        approverScopeElement.addAttribute(approverScopeValueAttribute);

        ExtensionAttribute approverScopeDescAttribute = new ExtensionAttribute();
        approverScopeDescAttribute.setName(ELEMENT_ATTRIBUTE_DESC);
        approverScopeDescAttribute.setValue("审批人所在范围");
        approverScopeElement.addAttribute(approverScopeDescAttribute);
        userTask.addExtensionElement(approverScopeElement);

        // 审批人指定
        ExtensionElement approverSpecifyElement = new ExtensionElement();
        approverSpecifyElement.setName(CONFIG_APPROVER_SPECIFY);

        ExtensionAttribute approverSpecifyValueAttribute = new ExtensionAttribute();
        approverSpecifyValueAttribute.setName(ELEMENT_ATTRIBUTE_VALUE);
        approverSpecifyValueAttribute.setValue(property.getApproverSpecify().getType());
        approverSpecifyElement.addAttribute(approverSpecifyValueAttribute);

        ExtensionAttribute approverSpecifyDescAttribute = new ExtensionAttribute();
        approverSpecifyDescAttribute.setName(ELEMENT_ATTRIBUTE_DESC);
        approverSpecifyDescAttribute.setValue("审批人指定");
        approverSpecifyElement.addAttribute(approverSpecifyDescAttribute);
        // 审批人指定的具体值
        approverSpecifyElement.setElementText(JSON.toJSONString(property.getSpecifyValue()));
        userTask.addExtensionElement(approverSpecifyElement);

        // 多人审批时审批模式
        ExtensionElement approverModeTypeElement = new ExtensionElement();
        approverModeTypeElement.setName(CONFIG_APPROVER_MODE_TYPE);

        ExtensionAttribute approverModeTypeValueAttribute = new ExtensionAttribute();
        approverModeTypeValueAttribute.setName(ELEMENT_ATTRIBUTE_VALUE);
        approverModeTypeValueAttribute.setValue(property.getMultiMode().getType());
        approverModeTypeElement.addAttribute(approverModeTypeValueAttribute);

        ExtensionAttribute approverModeTypeDescAttribute = new ExtensionAttribute();
        approverModeTypeDescAttribute.setName(ELEMENT_ATTRIBUTE_DESC);
        approverModeTypeDescAttribute.setValue("多人审批时审批模式");
        approverModeTypeElement.addAttribute(approverModeTypeDescAttribute);
        userTask.addExtensionElement(approverModeTypeElement);

        // 审批人为空时
        ExtensionElement approverEmptyHandleTypeElement = new ExtensionElement();
        approverEmptyHandleTypeElement.setName(CONFIG_APPROVER_EMPTY_HANDLE_TYPE);
        ExtensionAttribute approverEmptyHandleTypeValueAttribute = new ExtensionAttribute();
        approverEmptyHandleTypeValueAttribute.setName(ELEMENT_ATTRIBUTE_VALUE);
        approverEmptyHandleTypeValueAttribute.setValue(property.getApproverEmptyHandleType().getType());
        approverEmptyHandleTypeElement.addAttribute(approverEmptyHandleTypeValueAttribute);

        ExtensionAttribute approverEmptyHandleTypeDescAttribute = new ExtensionAttribute();
        approverEmptyHandleTypeDescAttribute.setName(ELEMENT_ATTRIBUTE_DESC);
        approverEmptyHandleTypeDescAttribute.setValue("审批人为空时");
        approverEmptyHandleTypeElement.addAttribute(approverEmptyHandleTypeDescAttribute);
        userTask.addExtensionElement(approverEmptyHandleTypeElement);
    }

    /**
     * 设置执行监听
     *
     * @param userTask
     */
    private static void setExecutionListeners(UserTask userTask) {

        List<FlowableListener> executionListeners = new ArrayList<>();
        // 设置执行监听
        FlowableListener executionListener = new FlowableListener();
        executionListener.setEvent(BaseExecutionListener.EVENTNAME_START);
        executionListener.setImplementationType(IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
        executionListener.setImplementation("${engineExecutionStartListener}");
        executionListeners.add(executionListener);

        userTask.setExecutionListeners(executionListeners);
    }

    /**
     * 设置任务监听
     *
     * @param userTask
     */
    private static void setTaskListeners(UserTask userTask) {
        List<FlowableListener> taskListeners = new ArrayList<>();
        // 设置任务监听
        FlowableListener taskListener = new FlowableListener();
        taskListener.setEvent(TaskListener.EVENTNAME_ALL_EVENTS);
        taskListener.setImplementationType(IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
        // FIXME 如果监听器想设置多个,这里还需要额外处理
        taskListener.setImplementation("${engineTaskEventListener}");
        taskListeners.add(taskListener);

        userTask.setTaskListeners(taskListeners);
    }

}
