package com.hotwj.platform.flow.callback;

import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 流程回调工厂
 *
 * @author shihongwei
 */
@Component
public class FlowCallbackFactory {

    private final Map<String, IFlowCallback> callbackMap = new ConcurrentHashMap<>();

    // Spring 自动注入所有实现类
    public FlowCallbackFactory(List<IFlowCallback> callbacks) {
        for (IFlowCallback callback : callbacks) {
            callbackMap.put(callback.getFlowCode(), callback);
        }
    }

    public void register(IFlowCallback callback) {
        callbackMap.put(callback.getFlowCode(), callback);
    }

    public Optional<IFlowCallback> getCallback(String flowCode) {
        return Optional.ofNullable(callbackMap.get(flowCode));
    }
}
