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 2024/2/5 18:46
 */
public enum ReceiveTaskEventType implements FlowableEventType {
    CREATE,
    UPDATE,
    ;
    public static final ReceiveTaskEventType[] EMPTY_ARRAY = new ReceiveTaskEventType[]{};

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