package com.ho.filecenter.config;

import cn.hutool.core.util.IdUtil;
import com.ho.filecenter.util.MqttConfigUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author fancl
 * @desc: Mqtt配置类 文件的先注释掉
 * @date 2022/11/7
 */
@Configuration
@Data
@ConfigurationProperties("mqtt")
@Slf4j
public class MqttConfig {

    //服务器url
    String url;
    //用户名
    String userName;
    //密码
    String passWord;
    //超时时间
    Integer timeout;
    //会话保持时间
    Integer keepAlive;

    @Value("${topic.edgeFileRequest}")
    String edgeFileRequest;

    @Value("${topic.edgeFileResponse}")
    String edgeFileResponse;

    @Value("${topic.cloudFileRequest}")
    String cloudFileRequest;

    @Value("${topic.cloudFileResponse}")
    String cloudFileResponse;

    @Value("${topic.edgeCurveResponse}")
    String edgeCurveResponse;

    @Value("${topic.edgeDispatchResponse}")
    String edgeDispatchResponse;

    @Autowired
    FileEdgeRequestConsumer fileEdgeRequestConsumer;

    @Autowired
    FileEdgeResponseConsumer fileEdgeResponseConsumer;

    @Autowired
    CurveResponseConsumer curveResponseConsumer;

    @Autowired
    DispatchResponseConsumer dispatchResponseConsumer;

    //todo 超时时间注释掉
    //文件请求
    @Bean(name = "FileEdgeRequest")
    public MqttClient initFileRequestClient() {
        String clientId = IdUtil.simpleUUID();
        log.info("clientId:" + clientId);
        MqttClient client = null;
        try {
            client = new MqttClient(url, clientId,null);
            MqttConnectOptions options = new MqttConnectOptions();
            options.setUserName(userName);
            options.setPassword(passWord.toCharArray());
            options.setCleanSession(true);
            options.setConnectionTimeout(timeout);
            options.setKeepAliveInterval(keepAlive);
            client.setCallback(fileEdgeRequestConsumer);
            IMqttToken iMqttToken = client.connectWithResult(options);
            boolean complete = iMqttToken.isComplete();
            log.info("FileRequestClient建立连接：{}", complete);

            //这里监听的是
            String[] topic = MqttConfigUtil.getFileRequestTopic();
            int[] qos = new int[topic.length];
            client.subscribe(topic,qos);
            log.info("已订阅topic：{}", topic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return client;
    }

    //文件响应
    @Bean(name = "FileEdgeResponse")
    public MqttClient initFileResponseClient() {
        String clientId = IdUtil.simpleUUID();
        log.info("clientId:" + clientId);
        MqttClient client =null;
        try {
            client = new MqttClient(url, clientId,null);
            MqttConnectOptions options = new MqttConnectOptions();
            options.setUserName(userName);
            options.setPassword(passWord.toCharArray());
            options.setCleanSession(true);
            options.setConnectionTimeout(timeout);
            options.setKeepAliveInterval(keepAlive);
            client.setCallback(fileEdgeResponseConsumer);
            IMqttToken iMqttToken = client.connectWithResult(options);
            boolean complete = iMqttToken.isComplete();
            log.info("FileResponseClient建立连接：{}", complete);

            //这里监听的是
            String[] topic = MqttConfigUtil.getFileResponseTopic();
            int[] qos = new int[topic.length];
            client.subscribe(topic,qos);
            log.info("已订阅topic：{}", topic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return client;
    }

    @Bean(name = "curveResponse")
    public MqttClient initCurveResponseClient() {
        String clientId = IdUtil.simpleUUID();
        log.info("clientId:" + clientId);
        MqttClient client =null;
        try {
            client = new MqttClient(url, clientId,null);
            MqttConnectOptions options = new MqttConnectOptions();
            options.setUserName(userName);
            options.setPassword(passWord.toCharArray());
            options.setCleanSession(true);
            options.setConnectionTimeout(timeout);
            options.setKeepAliveInterval(keepAlive);
            client.setCallback(curveResponseConsumer);
            IMqttToken iMqttToken = client.connectWithResult(options);
            boolean complete = iMqttToken.isComplete();
            log.info("FileRequestClient建立连接：{}", complete);

            //这里监听的是
            String[] topic = MqttConfigUtil.getCurveResponseTopic();
            int[] qos = new int[topic.length];
            client.subscribe(topic,qos);
            log.info("已订阅topic：{}", topic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return client;
    }

    @Bean(name = "dispatchResponse")
    public MqttClient initDispatchResponseClient() {
        String clientId = IdUtil.simpleUUID();
        log.info("clientId:" + clientId);
        MqttClient client =null;
        try {
            client = new MqttClient(url, clientId,null);
            MqttConnectOptions options = new MqttConnectOptions();
            options.setUserName(userName);
            options.setPassword(passWord.toCharArray());
            options.setCleanSession(true);
            options.setConnectionTimeout(timeout);
            options.setKeepAliveInterval(keepAlive);
            client.setCallback(dispatchResponseConsumer);
            IMqttToken iMqttToken = client.connectWithResult(options);
            boolean complete = iMqttToken.isComplete();
            log.info("FileRequestClient建立连接：{}", complete);

            //这里监听的是
            String[] topic = MqttConfigUtil.getDispatchResponseTopic();
            int[] qos = new int[topic.length];
            client.subscribe(topic,qos);
            log.info("已订阅topic：{}", topic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return client;
    }
}
