package com.njzscloud.common.mqtt.util;

import cn.hutool.core.map.MapUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.njzscloud.common.mqtt.support.MqttCliWrapper;
import com.njzscloud.common.mqtt.support.MqttMsg;

import java.util.function.Consumer;

public final class Mqtt {
    private static final MqttCliWrapper MQTT_CLI_WRAPPER;

    static {
        MQTT_CLI_WRAPPER = SpringUtil.getBean(MqttCliWrapper.class);
    }

    public static void subscribe(String topic, int qos, Consumer<MqttMsg> handler) {
        MQTT_CLI_WRAPPER.subscribe(topic, qos, handler);
    }

    public static void subscribe(String topic, Consumer<MqttMsg> handler) {
        MQTT_CLI_WRAPPER.subscribe(topic, handler);
    }

    public static void subscribe(String topic, int qos) {
        MQTT_CLI_WRAPPER.subscribe(topic, qos);
    }

    public static void subscribe(String topic) {
        MQTT_CLI_WRAPPER.subscribe(topic);
    }


    public static void publish(String topic, Object msg) {
        MQTT_CLI_WRAPPER.publish(topic, msg);
    }

    public static void publish(String topic) {
        MQTT_CLI_WRAPPER.publish(topic, MapUtil.builder()
                .put("topic", topic)
                .build());
    }

    public static void publish(String topic, int qos, Object msg) {
        MQTT_CLI_WRAPPER.publish(topic, qos, msg);
    }

    public static void unsubscribe(String topic) {
        MQTT_CLI_WRAPPER.unsubscribe(topic);
    }
}
