package com.multictrl.modules.business.handler;

import cn.hutool.core.util.StrUtil;
import com.multictrl.common.constant.BusinessConstant;
import com.multictrl.modules.business.q20.handler.Q20EventsHandler;
import com.multictrl.modules.business.q20.handler.Q20OsdTopicHandler;
import com.multictrl.modules.business.q20.handler.Q20StateTopicHandler;
import com.multictrl.modules.business.q20.handler.Q20StatusHandler;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * mqtt client1主题分发器
 *
 * @author Sdy
 * @since 1.0.0 2026/4/22
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class TopicDistributor {
    private final RequestsHandler requestsHandler;
    private final StatusHandler statusHandler;
    private final OsdHandler osdHandler;
    private final StateHandler stateHandler;
    private final ServicesReplyHandler servicesReplyHandler;
    private final EventsHandler eventsHandler;
    private final Q20OsdTopicHandler q20OsdTopicHandler;
    private final Q20EventsHandler q20EventsHandler;
    private final Q20StateTopicHandler q20StateTopicHandler;
    private final Q20StatusHandler q20StatusHandler;
    private final Map<String, MessageHandler> handlerMap = new ConcurrentHashMap<>();
    private final Map<String, MessageHandler> q20HandlerMap = new ConcurrentHashMap<>();

    private static final String Q20_THING_PREFIX = "thing/device/";
    private static final String Q20_SYS_PREFIX = "sys/device/";

    @PostConstruct
    public void init() {
        handlerMap.put(BusinessConstant.REQUESTS, requestsHandler);
        handlerMap.put(BusinessConstant.STATUS, statusHandler);
        handlerMap.put(BusinessConstant.OSD, osdHandler);
        handlerMap.put(BusinessConstant.STATE, stateHandler);
        handlerMap.put(BusinessConstant.EVENTS, eventsHandler);
        handlerMap.put(BusinessConstant.SERVICES_REPLY, servicesReplyHandler);

        q20HandlerMap.put(BusinessConstant.OSD, q20OsdTopicHandler);
        q20HandlerMap.put(BusinessConstant.EVENTS, q20EventsHandler);
        q20HandlerMap.put(BusinessConstant.STATE, q20StateTopicHandler);
        q20HandlerMap.put(BusinessConstant.STATUS, q20StatusHandler);
        q20HandlerMap.put(BusinessConstant.SERVICES_REPLY, servicesReplyHandler);
    }

    public void route(String topic, String payload) {
        String method = StrUtil.subAfter(topic, "/", true);
        String gateway = StrUtil.subAfter(StrUtil.subBefore(topic, "/", true),
                "/", true);
        boolean isQ20 = topic.startsWith(Q20_THING_PREFIX) || topic.startsWith(Q20_SYS_PREFIX);
        Map<String, MessageHandler> map = isQ20 ? q20HandlerMap : handlerMap;
        MessageHandler handler = map.get(method);
        if (handler != null) {
            handler.handleMessage(topic, payload, gateway);
        } else {
//            log.error("未找到对应的处理器: {}", topic);
        }
    }
}
