package com.seer.teach.teacher.service.registry.updateLearningDuration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * <p>
 *
 * </p >
 *
 * @author Captain
 * @since 2025-09-20 16:09
 */
@Slf4j
@Component
public class UpdateLearningDurationHandlerRegistry {

    private final Map<Integer, UpdateLearningDurationHandler> handlerMap = new HashMap<>();

    @Autowired
    public UpdateLearningDurationHandlerRegistry(List<UpdateLearningDurationHandler> handlers) {
        // 遍历所有处理器实例，按类型注册到映射表中
        for (UpdateLearningDurationHandler handler : handlers) {
            Integer type = handler.getType();
            // 检查是否已存在相同类型的处理器，避免重复注册
            if (handlerMap.containsKey(type)) {
                log.warn("重复注册处理器 tag={}", type);
            }
            handlerMap.put(type, handler);
            log.info("注册错题详情查询器 type={}，handler={}", type, handler.getClass().getSimpleName());
        }
    }

    public UpdateLearningDurationHandler getHandler(Integer type) {
        return handlerMap.get(type);
    }

}
