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;

/**
 * 业务节点指定审批人事件类型
 * <p>
 * 在现阶段,可能仅有一种事件类型,就是告知业务请传递审批人, 后续还可能会有变更,移除,添加等等类型
 *
 * @author wangli
 * @since 2023/12/21 10:03
 */
public enum BizSpecifyAssigneeEventType implements FlowableEventType {
    ADD_ASSIGNEE,
    // 下面两个没有实际场景,只是个预留的扩展方式,后续如新增了事件类型,可以直接改造
    UPDATE_ASSIGNEE,
    REMOVE_ASSIGNEE,
    ;
    public static final BizSpecifyAssigneeEventType[] EMPTY_ARRAY = new BizSpecifyAssigneeEventType[]{};

    /**
     * @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 BizSpecifyAssigneeEventType[] getTypesFromString(String string) {
        List<BizSpecifyAssigneeEventType> result = new ArrayList<>();
        if (string != null && !string.isEmpty()) {
            String[] split = StringUtils.split(string, ",");
            for (String typeName : split) {
                boolean found = false;
                for (BizSpecifyAssigneeEventType 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);
    }
}
