package com.seer.teach.netty.im.registry;

import com.seer.teach.netty.common.annotation.ImMessageContentType;
import com.seer.teach.netty.im.handler.im.ImMessageContentHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @Author: Captain
 * @Description: Tv端业务处理器
 * @Date: 2025-05-23 14:48
 */
@Component
public class TvSingleChatBusinessHandlerRegistry {

    private final Map<Integer, ImMessageContentHandler> handlerMap = new ConcurrentHashMap<>();

    @Autowired
    public TvSingleChatBusinessHandlerRegistry(List<ImMessageContentHandler> handlers) {
        for (ImMessageContentHandler handler : handlers) {
            ImMessageContentType annotation = handler.getClass().getAnnotation(ImMessageContentType.class);
            if (annotation != null) {
                int type = annotation.value();
                if (handlerMap.containsKey(type)) {
                    throw new IllegalStateException("重复注册的消息类型: " + type);
                }
                handlerMap.put(type, handler);
            }
        }
    }

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