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

import cn.axzo.workflow.common.exception.WorkflowEngineException;
import cn.axzo.workflow.common.model.request.BpmnApproveConf;
import cn.axzo.workflow.common.model.request.bpmn.BpmnNoticeConf;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskDelegateAssigner;

import java.util.List;

import static cn.axzo.workflow.common.code.OtherRespCode.MESSAGE_PUSH_EVENT_BUILD_ERROR;
import static cn.axzo.workflow.common.code.OtherRespCode.MES_PUSH_OBJECT_BUILD_ERROR;
import static cn.axzo.workflow.core.engine.event.MessagePushEventType.CARBON_COPY;
import static cn.axzo.workflow.core.engine.event.MessagePushEventType.CARBON_COPY_COMPLETE;
import static cn.axzo.workflow.core.engine.event.MessagePushEventType.IM;
import static cn.axzo.workflow.core.engine.event.MessagePushEventType.NOTICE;
import static cn.axzo.workflow.core.engine.event.MessagePushEventType.PENDING_COMPLETE;
import static cn.axzo.workflow.core.engine.event.MessagePushEventType.PENDING_PUSH;
import static cn.axzo.workflow.core.engine.event.MessagePushEventType.PENDING_ROLLBACK;
import static cn.axzo.workflow.core.engine.event.MessagePushEventType.SMS;

/**
 * 推送事件对象构造器
 *
 * @author wangli
 * @since 2023/11/22 14:16
 */
public class MessagePushEventBuilder {

    public static MessagePushEventImpl createEvent(MessagePushEventType type, List<BpmnTaskDelegateAssigner> assigners,
                                                   BpmnNoticeConf noticeConf, String processInstanceId, String processDefinitionKey,
                                                   String tenantId, String taskId, Boolean activitySignature) {
        return createEvent(type, assigners, noticeConf, processInstanceId, processDefinitionKey, tenantId, taskId, activitySignature, null, null);
    }
    public static MessagePushEventImpl createEvent(MessagePushEventType type, List<BpmnTaskDelegateAssigner> assigners,
                                                   BpmnNoticeConf noticeConf, String processInstanceId, String processDefinitionKey,
                                                   String tenantId, String taskId, Boolean activitySignature, String imTemplateCode, String terminalType) {
        switch (type) {
            case NOTICE:
                return createNoticeEvent(assigners, noticeConf, processInstanceId, processDefinitionKey, tenantId, taskId, activitySignature);
            case PENDING_PUSH:
                throw new WorkflowEngineException(MESSAGE_PUSH_EVENT_BUILD_ERROR);
            case PENDING_COMPLETE:
                return createPendingCompleteEvent(assigners, noticeConf, processInstanceId, processDefinitionKey, tenantId, taskId, activitySignature);
            case CARBON_COPY:
                return createCarbonCopyEvent(assigners, noticeConf, processInstanceId, processDefinitionKey, tenantId, taskId, activitySignature);
            case CARBON_COPY_COMPLETE:
                return createCarbonCopyCompleteEvent(assigners, noticeConf, processInstanceId, processDefinitionKey, tenantId, activitySignature);
            case SMS:
                return createSmsEvent(assigners, noticeConf, processInstanceId, processDefinitionKey, tenantId, taskId, activitySignature);
            case IM:
                return createImEvent(assigners, imTemplateCode, processInstanceId, terminalType);
            default:
                throw new WorkflowEngineException(MES_PUSH_OBJECT_BUILD_ERROR);
        }
    }

    public static MessagePushEventImpl createNoticeEvent(List<BpmnTaskDelegateAssigner> assigners,
                                                         BpmnNoticeConf noticeConf
            , String processInstanceId, String processDefinitionKey, String tenantId, String taskId, Boolean activitySignature) {
        MessagePushEventImpl newEvent = new MessagePushEventImpl(NOTICE, assigners, noticeConf, processInstanceId,
                processDefinitionKey, tenantId, taskId);
        newEvent.setActivitySignature(activitySignature);
        return newEvent;
    }

    public static MessagePushEventImpl createPendingPushEvent(List<BpmnTaskDelegateAssigner> assigners,
                                                              BpmnNoticeConf noticeConf,
                                                              BpmnApproveConf processApproveConf,
                                                              String processInstanceId,
                                                              String processDefinitionId,
                                                              String processDefinitionKey,
                                                              String currentTaskDefinitionKey,
                                                              String tenantId, String taskId,
                                                              Boolean activitySignature) {
        MessagePushEventImpl newEvent = new MessagePushEventImpl(PENDING_PUSH, assigners, noticeConf, processApproveConf,
                processInstanceId, processDefinitionId, processDefinitionKey, currentTaskDefinitionKey, tenantId, taskId);
        newEvent.setActivitySignature(activitySignature);
        return newEvent;
    }

    public static MessagePushEventImpl createPendingCompleteEvent(List<BpmnTaskDelegateAssigner> assigners,
                                                                  BpmnNoticeConf noticeConf, String processInstanceId,
                                                                  String processDefinitionKey,
                                                                  String tenantId, String taskId, Boolean activitySignature) {
        MessagePushEventImpl newEvent = new MessagePushEventImpl(PENDING_COMPLETE, assigners, noticeConf,
                processInstanceId, processDefinitionKey, tenantId, taskId);
        newEvent.setActivitySignature(activitySignature);
        return newEvent;
    }

    public static MessagePushEvent createPendingRollbackEvent(String processInstanceId, String processDefinitionKey,
                                                              String tenantId, String taskId, BpmnNoticeConf noticeConf) {
        return new MessagePushEventImpl(PENDING_ROLLBACK, null, noticeConf, processInstanceId, processDefinitionKey, tenantId, taskId);
    }

    public static MessagePushEventImpl createCarbonCopyEvent(List<BpmnTaskDelegateAssigner> assigners,
                                                             BpmnNoticeConf noticeConf, String processInstanceId,
                                                             String processDefinitionKey,
                                                             String tenantId,
                                                             String taskId,
                                                             Boolean activitySignature) {
        MessagePushEventImpl newEvent = new MessagePushEventImpl(CARBON_COPY, assigners, noticeConf,
                processInstanceId, processDefinitionKey, tenantId, taskId);
        newEvent.setActivitySignature(activitySignature);
        return newEvent;
    }

    public static MessagePushEventImpl createCarbonCopyCompleteEvent(List<BpmnTaskDelegateAssigner> assigners,
                                                                     BpmnNoticeConf noticeConf, String processInstanceId,
                                                                     String processDefinitionKey,
                                                                     String tenantId, Boolean activitySignature) {
        MessagePushEventImpl newEvent = new MessagePushEventImpl(CARBON_COPY_COMPLETE, assigners, noticeConf,
                processInstanceId, processDefinitionKey, tenantId, null);
        newEvent.setActivitySignature(activitySignature);
        return newEvent;
    }

    public static MessagePushEventImpl createSmsEvent(List<BpmnTaskDelegateAssigner> assigners,
                                                      BpmnNoticeConf noticeConf,
                                                      String processInstanceId,
                                                      String processDefinitionKey,
                                                      String tenantId, String taskId, Boolean activitySignature) {
        MessagePushEventImpl newEvent = new MessagePushEventImpl(SMS, assigners, noticeConf, processInstanceId,
                processDefinitionKey, tenantId, taskId);
        newEvent.setActivitySignature(activitySignature);
        return newEvent;
    }

    public static MessagePushEventImpl createImEvent(List<BpmnTaskDelegateAssigner> assigners,
                                                     String imTemplateCode,
                                                     String processInstanceId,
                                                     String terminalType) {
        return new MessagePushEventImpl(IM, assigners, imTemplateCode, processInstanceId, terminalType);
    }
}
