import {
    connect as mqttConnect
} from 'mqtt'
export default class TslMqttClient {
    // 接收消息的所有回调
    onMessageCallBacks = [];
    // 订阅的所有消息
    subscribeTopics = [];
    // 所有订阅
    constructor() {
        if (!TslMqttClient.instance) {
            TslMqttClient.instance = this
        }

        return TslMqttClient.instance
    }

    static getInstance() {
        if (!this.instance) {
            this.instance = new TslMqttClient()
        }
        return this.instance
    }

    connectMqtt(url, opti) {
        let _that = this
        if (!this.mqttClient) {
            this.mqttClient = mqttConnect(url, opti)
            this.mqttClient.on('message', function (topic, payload) {
                _that.onMessageCallBacks.forEach((callback) => {
                    callback(topic, payload.toString())
                })
            })

            // 刚初始化的时候mqttClient为空
            this.mqttClient.on('connect', function () {
                _that.subscribeTopics.forEach(topic => {
                    _that.mqttClient.subscribe(topic)
                })
            })
        }
    }

    subscribe(topic) {
        // topic 是数组
        if (topic instanceof Array) {
            for (let index = 0; index < topic.length; index++) {
                const element = topic[index];

                if (!this.subscribeTopics.some(x => x === element)) {
                    this.subscribeTopics.push(element)
                }
                if (this.mqttClient) {
                    this.mqttClient.subscribe(element)
                }
            }

            return
        }

        // 单个topic
        if (!this.subscribeTopics.some(x => x === topic)) {
            this.subscribeTopics.push(topic)
        }
        if (this.mqttClient) {
            this.mqttClient.subscribe(topic)
        }
    }

    unsubscribe(topic) {
        // topic 是数组
        if (topic instanceof Array) {
            if (this.mqttClient) {
                // 如果存在，则删除数组中
                for (let index = 0; index < topic.length; index++) {
                    const element = topic[index];

                    let index = this.subscribeTopics.findIndex(x => x === element)
                    if (index > -1) {
                        this.subscribeTopics.splice(index, 1)
                    }
                    this.mqttClient.unsubscribe(element)
                }

            }

            return
        }
        if (this.mqttClient) {
            // 如果存在，则删除数组中
            let index = this.subscribeTopics.findIndex(x => x === topic)
            if (index > -1) {
                this.subscribeTopics.splice(index, 1)
            }
            this.mqttClient.unsubscribe(topic)
        }
    }

    onMessage(callback) {
        this.onMessageCallBacks.push(callback)
    }

    publish(topic, message, options, callback) {
        if (this.mqttClient) {
            this.mqttClient.publish(topic, message, options, callback)
        }
    }

    end() {
        if (this.mqttClient) {
            this.mqttClient.end()
            this.onMessageCallBacks = []
            this.subscribeTopics = []
        }
    }
}