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

import cn.axzo.workflow.common.model.request.bpmn.BpmnCarbonCopyConf;
import cn.axzo.workflow.common.model.request.bpmn.BpmnJsonNode;
import org.flowable.bpmn.model.ExtensionAttribute;
import org.flowable.bpmn.model.ExtensionElement;
import org.flowable.bpmn.model.FlowableListener;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.ServiceTask;
import org.springframework.util.CollectionUtils;

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

import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_CARBON_COPIES;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_CARBON_COPY;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_CARBON_COPY_OBJECT;
import static cn.axzo.workflow.common.constant.BpmnConstants.CONFIG_CARBON_COPY_SPECIFY;
import static cn.axzo.workflow.common.constant.BpmnConstants.ELEMENT_ATTRIBUTE_DESC;
import static cn.axzo.workflow.common.constant.BpmnConstants.ELEMENT_ATTRIBUTE_NAME;
import static cn.axzo.workflow.common.constant.BpmnConstants.ELEMENT_ATTRIBUTE_VALUE;
import static org.flowable.bpmn.model.ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION;

/**
 * 服务任务节点, 目前这个类型主要用来做抄送功能
 *
 * @author wangli
 * @since 2023/10/17 15:47
 */
public class ServiceTaskJsonConverter extends AbstractBpmnJsonConverter<ServiceTask> {
    @Override
    public ServiceTask convertJsonToElement(BpmnJsonNode node, Process process) {
        ServiceTask serviceTask = new ServiceTask();
        serviceTask.setId(node.getId());
        serviceTask.setName(node.getName());

        // 设置抄送具体的实现类
        setCarbonCopyDelegate(serviceTask);

        // 持久化节点抄送配置元数据
        setCarbonCopyExtensionElement(node, serviceTask);
        return serviceTask;
    }

    private static void setCarbonCopyExtensionElement(BpmnJsonNode node, ServiceTask serviceTask) {
        if (Objects.isNull(node.getProperty())) {
            return;
        }
        List<BpmnCarbonCopyConf> configs = node.getProperty().getCarbonCopyConf();
        ExtensionElement carbonCopyConfigsElement = new ExtensionElement();
        carbonCopyConfigsElement.setName(CONFIG_CARBON_COPIES);

        if (!CollectionUtils.isEmpty(configs)) {
            configs.forEach(i -> {
                ExtensionElement carbonCopyConfigElement = new ExtensionElement();
                carbonCopyConfigElement.setName(CONFIG_CARBON_COPY);

                buildCarbonCopyObject(i, carbonCopyConfigElement);
                buildCarbonCopySpecify(i, carbonCopyConfigElement);
                //                buildCarbonCopyTemplate(i, carbonCopyConfigElement);

                carbonCopyConfigsElement.addChildElement(carbonCopyConfigElement);
            });
        }

        serviceTask.addExtensionElement(carbonCopyConfigsElement);
    }

    //    private static void buildCarbonCopyTemplate(BpmnCarbonCopyConf i, ExtensionElement carbonCopyConfigElement) {
    //        ExtensionElement carbonCopyTemplateElement = new ExtensionElement();
    //        carbonCopyTemplateElement.setName(CONFIG_CARBON_COPY_TEMPLATE_CODE);
    //
    //        ExtensionAttribute pushMessageAttribute = new ExtensionAttribute();
    //        pushMessageAttribute.setName(ELEMENT_ATTRIBUTE_VALUE);
    //        pushMessageAttribute.setValue(i.getTemplateCode());
    //        carbonCopyTemplateElement.addAttribute(pushMessageAttribute);
    //
    //        ExtensionAttribute pushMessageDescAttribute = new ExtensionAttribute();
    //        pushMessageDescAttribute.setName(ELEMENT_ATTRIBUTE_DESC);
    //        pushMessageDescAttribute.setValue("抄送消息模板");
    //        carbonCopyTemplateElement.addAttribute(pushMessageDescAttribute);
    //
    //        carbonCopyConfigElement.addChildElement(carbonCopyTemplateElement);
    //    }

    private static void buildCarbonCopySpecify(BpmnCarbonCopyConf i, ExtensionElement carbonCopyConfigElement) {
        ExtensionElement carbonCopySpecifyElement = new ExtensionElement();
        carbonCopySpecifyElement.setName(CONFIG_CARBON_COPY_SPECIFY);

        ExtensionAttribute specifyValueAttribute = new ExtensionAttribute();
        specifyValueAttribute.setName(ELEMENT_ATTRIBUTE_DESC);
        specifyValueAttribute.setValue("具体抄送对象值");
        carbonCopySpecifyElement.setElementText(i.getSpecifyValue());
        carbonCopySpecifyElement.addAttribute(specifyValueAttribute);

        carbonCopyConfigElement.addChildElement(carbonCopySpecifyElement);
    }

    private static void buildCarbonCopyObject(BpmnCarbonCopyConf i, ExtensionElement carbonCopyConfigElement) {
        ExtensionElement carbonCopyObjectElement = new ExtensionElement();
        carbonCopyObjectElement.setName(CONFIG_CARBON_COPY_OBJECT);

        ExtensionAttribute objectValueAttribute = new ExtensionAttribute();
        objectValueAttribute.setName(ELEMENT_ATTRIBUTE_VALUE);
        objectValueAttribute.setValue(i.getCarbonCopyObjectType().getType());
        carbonCopyObjectElement.addAttribute(objectValueAttribute);

        ExtensionAttribute objectNameAttribute = new ExtensionAttribute();
        objectNameAttribute.setName(ELEMENT_ATTRIBUTE_NAME);
        objectNameAttribute.setValue(i.getCarbonCopyObjectType().getDesc());
        carbonCopyObjectElement.addAttribute(objectNameAttribute);

        ExtensionAttribute descAttribute = new ExtensionAttribute();
        descAttribute.setName(ELEMENT_ATTRIBUTE_DESC);
        descAttribute.setValue("抄送对象类型");
        carbonCopyObjectElement.addAttribute(descAttribute);

        carbonCopyConfigElement.addChildElement(carbonCopyObjectElement);
    }

    private static void setCarbonCopyDelegate(ServiceTask serviceTask) {
        //        serviceTask.setAsynchronous(true);
        serviceTask.setImplementation("${engineCarbonCopyEventListener}");
        serviceTask.setImplementationType(IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
    }

    private static void setExecutionListeners(ServiceTask serviceTask) {

        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);
        //
        //        FlowableListener activityStartListener = new FlowableListener();
        //        activityStartListener.setEvent(BaseExecutionListener.EVENTNAME_START);
        //        activityStartListener.setImplementationType(IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
        //        activityStartListener.setImplementation("${engineActivityStartEventListener}");
        //        executionListeners.add(activityStartListener);
        //
        //        FlowableListener activityTakeListener = new FlowableListener();
        //        activityTakeListener.setEvent(BaseExecutionListener.EVENTNAME_TAKE);
        //        activityTakeListener.setImplementationType(IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
        //        activityTakeListener.setImplementation("${engineActivityTakeEventListener}");
        //        executionListeners.add(activityTakeListener);
        //
        //        FlowableListener activityEndListener = new FlowableListener();
        //        activityEndListener.setEvent(BaseExecutionListener.EVENTNAME_END);
        //        activityEndListener.setImplementationType(IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
        //        activityEndListener.setImplementation("${engineActivityEndEventListener}");
        //        executionListeners.add(activityEndListener);

        serviceTask.setExecutionListeners(executionListeners);
    }
}
