package com.multictrl.modules.business.q20.handler;

import cn.hutool.json.JSONObject;
import com.multictrl.common.constant.BusinessConstant;
import com.multictrl.common.utils.JsonUtils;
import com.multictrl.modules.business.handler.MessageHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * Q20设备OSD主题分发器
 * 接收 thing/device/{device_sn}/osd 主题消息，根据payload中的method字段分发到对应处理器
 * method: osd(基础遥测) | rtk(RTK定位) | rc(遥控链路) | mount(负载) | battery(电池) | obs(避障)
 *
 * @author 938693313@qq.com
 * @since 1.0.0 2026/5/12
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class Q20OsdTopicHandler implements MessageHandler {

    private final Q20OsdHandler q20OsdHandler;
    private final Q20RtkHandler q20RtkHandler;
    private final Q20RcHandler q20RcHandler;
    private final Q20MountHandler q20MountHandler;
    private final Q20BatteryHandler q20BatteryHandler;
    private final Q20ObsHandler q20ObsHandler;

    @Override
    public void handleMessage(String topic, String payload, String gateway) {
        JSONObject message = JsonUtils.parseObject(payload, JSONObject.class);
        if (message == null) {
            log.debug("Q20 osd topic --> payload解析失败，解析后为null");
            return;
        }
        String method = message.getStr(BusinessConstant.METHOD);
        if (method == null) {
            log.debug("Q20 osd topic --> method字段缺失, topic: {}", topic);
            return;
        }
        switch (method) {
            case Q20Constant.METHOD_OSD:
                q20OsdHandler.handleMessage(topic, payload, gateway);
                break;
            case Q20Constant.METHOD_RTK:
                q20RtkHandler.handleMessage(topic, payload, gateway);
                break;
            case Q20Constant.METHOD_RC:
                q20RcHandler.handleMessage(topic, payload, gateway);
                break;
            case Q20Constant.METHOD_MOUNT:
                q20MountHandler.handleMessage(topic, payload, gateway);
                break;
            case Q20Constant.METHOD_BATTERY:
                q20BatteryHandler.handleMessage(topic, payload, gateway);
                break;
            case Q20Constant.METHOD_OBS:
                q20ObsHandler.handleMessage(topic, payload, gateway);
                break;
            default:
                log.debug("Q20 osd topic --> 未知method: {}", method);
        }
    }
}
