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

import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
import org.flowable.common.engine.api.delegate.event.FlowableEventType;

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

/**
 * 消息推送事件类型
 *
 * @author wangli
 * @since 2023/11/22 14:06
 */
public enum MessagePushEventType implements FlowableEventType {

    /**
     * 推送站内信
     */
    NOTICE,
    /**
     * 推送待办
     */
    PENDING_PUSH,
    /**
     * 完成待办
     */
    PENDING_COMPLETE,
    /**
     * 抄送消息
     */
    CARBON_COPY,
    /**
     * 完成抄送消息
     */
    CARBON_COPY_COMPLETE,
    /**
     * 推送短信
     */
    SMS,
    ;

    public static final MessagePushEventType[] EMPTY_ARRAY = new MessagePushEventType[]{};

    /**
     * @param string the string containing a comma-separated list of event-type names
     * @return a list of FlowableEngineEventType based on the given list.
     * @throws FlowableIllegalArgumentException when one of the given string is not a valid type name
     */
    public static MessagePushEventType[] getTypesFromString(String string) {
        List<MessagePushEventType> result = new ArrayList<>();
        if (string != null && !string.isEmpty()) {
            String[] split = StringUtils.split(string, ",");
            for (String typeName : split) {
                boolean found = false;
                for (MessagePushEventType type : values()) {
                    if (typeName.toUpperCase().equals(type.name())) {
                        result.add(type);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    throw new FlowableIllegalArgumentException("Invalid event-type: " + typeName);
                }
            }
        }

        return result.toArray(EMPTY_ARRAY);
    }
}
