using HslCommunication;
using HslCommunication.MQTT;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class MQTTInitData
{
    public string ip = "172.16.1.24";
    public int port = 54232;
    public string userName = "server1234";
    public string passwd = "123456";
    [NonSerialized]
    public string guid = "";

    public void LoadConfig()
    {
        try
        {
            var path = Path.Combine(Application.streamingAssetsPath, "mqttConfig.json");
            if (File.Exists(path))
            {
                var jsonStr = File.ReadAllText(path);
                var config = JsonConvert.DeserializeObject<MQTTInitData>(jsonStr);
                this.ip = config.ip;
                this.port = config.port;
                this.userName = config.userName;
                this.passwd = config.passwd;
            }

        }
        catch (Exception ex)
        {
            Debug.LogException(ex);
        }
        this.guid = Guid.NewGuid().ToString("N");
    }

    public MqttConnectionOptions ToContentOptions()
    {
        MqttConnectionOptions options = new MqttConnectionOptions()
        {
            IpAddress = ip,
            Port = port,
            ClientId = guid,
            Credentials = new MqttCredential()
            {
                UserName = userName,
                Password = passwd,
            }
        };

        return options;
    }
}