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;
import java.util.Map;

/**
 * 业务节点回调事件
 *
 * @author wangli
 * @since 2024-08-20 17:25
 */
public class BizCallbackEventImpl implements BizCallbackEvent {
    private final BizCallbackEventType type;
    private final String activityId;
    private final String activityName;
    private final String processInstanceId;
    private final String processDefinitionId;
    private final String businessKey;
    private final String executionId;
    private final Map<String, Object> variables;

    public BizCallbackEventImpl(BizCallbackEventType type, String activityId, String activityName, String processInstanceId, String processDefinitionId, String businessKey, String executionId, Map<String, Object> variables) {
        this.type = type;
        this.activityId = activityId;
        this.activityName = activityName;
        this.processInstanceId = processInstanceId;
        this.processDefinitionId = processDefinitionId;
        this.businessKey = businessKey;
        this.executionId = executionId;
        this.variables = variables;
    }

    /**
     * @return type of event.
     */
    @Override
    public FlowableEventType getType() {
        return BizCallbackEventType.CALLBACK;
    }

    @Override
    public String getActivityId() {
        return activityId;
    }

    @Override
    public String getActivityName() {
        return activityName;
    }

    @Override
    public String getProcessInstanceId() {
        return processInstanceId;
    }

    @Override
    public String getProcessDefinitionId() {
        return processDefinitionId;
    }

    @Override
    public String getBusinessKey() {
        return businessKey;
    }

    @Override
    public String getExecutionId() {
        return executionId;
    }

    @Override
    public Map<String, Object> getVariables() {
        return variables;
    }

    public enum BizCallbackEventType implements FlowableEventType {
        CALLBACK,
        ;
        public static final BizCallbackEventType[] EMPTY_ARRAY = new BizCallbackEventType[]{};

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