package com.fuyuanshen.global.mqtt.utils;

import com.fuyuanshen.global.mqtt.base.MqttTopicInfo;
import lombok.experimental.UtilityClass;

/**
 * MQTT主题处理工具类
 */
@UtilityClass
public class MqttTopicUtils {
    
    public static final String COMMAND_PREFIX = "command";
    public static final String STATUS_PREFIX = "status";
    public static final String REPORT_PREFIX = "report";
    
    /**
     * 构建下发指令主题
     * @param tenantCode 租户编码
     * @param deviceType 设备类型
     * @param imei 设备IMEI
     * @return 主题字符串
     */
    public static String buildCommandTopic(String tenantCode, String deviceType, String imei) {
        return String.format("%s/%s/%s/%s", COMMAND_PREFIX, tenantCode, deviceType, imei);
    }
    
    /**
     * 构建响应数据主题
     * @param tenantCode 租户编码
     * @param deviceType 设备类型
     * @param imei 设备IMEI
     * @return 主题字符串
     */
    public static String buildStatusTopic(String tenantCode, String deviceType, String imei) {
        return String.format("%s/%s/%s/%s", STATUS_PREFIX, tenantCode, deviceType, imei);
    }
    
    /**
     * 构建设备上报数据主题
     * @param tenantCode 租户编码
     * @param deviceType 设备类型
     * @param imei 设备IMEI
     * @return 主题字符串
     */
    public static String buildReportTopic(String tenantCode, String deviceType, String imei) {
        return String.format("%s/%s/%s/%s", REPORT_PREFIX, tenantCode, deviceType, imei);
    }
    
    /**
     * 解析MQTT主题
     * @param topic 主题字符串
     * @return 主题信息对象
     */
    public static MqttTopicInfo parseTopic(String topic) {
        if (topic == null || topic.isEmpty()) {
            return null;
        }
        
        String[] parts = topic.split("/");
        if (parts.length != 4) {
            return null;
        }
        
        MqttTopicInfo info = new MqttTopicInfo();
        info.setOperation(parts[0]);
        info.setTenantCode(parts[1]);
        info.setDeviceType(parts[2]);
        info.setImei(parts[3]);
        return info;
    }
}