using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using BPA.Utility.Consul.YT;
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Consul;
using System.Threading;
using HBLConsole.Service;

namespace HBLConsole.GVL
{
    /// <summary>
    /// 网络信息数据
    /// </summary>
    public class InternetInfo
    {
        //private volatile static InternetInfo _Instance;
        //public static InternetInfo GetInstance => _Instance ?? (_Instance = new InternetInfo());
        //private InternetInfo() { }

        /// <summary>
        /// 配置初始化
        /// </summary>
        public static void ConfigInit()
        {
            NetworkConnectState = UniversalHelper.GetInstance.GetNetworkState();
            while (!NetworkConnectState)
            {
                NetworkConnectState = UniversalHelper.GetInstance.GetNetworkState();
                Thread.Sleep(2000);
            }

            ConsulAddress = System.Configuration.ConfigurationManager.AppSettings["ConsulAddress"];
            ClientId = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ClientId"]);
            ProductKey = System.Configuration.ConfigurationManager.AppSettings["ProductKey"];
            DeviceName = System.Configuration.ConfigurationManager.AppSettings["DeviceName"];
            DeviceSecret = System.Configuration.ConfigurationManager.AppSettings["DeviceSecret"];
            DeviceMS= System.Configuration.ConfigurationManager.AppSettings["DeviceMS"];
            while (StockServer == null)
            {
                try
                {
                    IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddConsul(ConsulAddress, "root/basic.json", false, 5_000);
                    IConfiguration config = configurationBuilder.Build();
                    MqttUserName = config["MQTT:TcpAccount"];
                    MqttPassword = config["MQTT:TcpPwd"];
                    MqttAddress = config["MQTT:IP"];
                    MqttPort = int.Parse(config["MQTT:Client"]);
                    ApiAddress = config["GateWay:BaseURL"];
                    OrderServer = config["GateWay:Order"];
                    StockServer = config["GateWay:Stock"];
                }
                catch (Exception ex)
                {
                    MessageLog.GetInstance.Show(ex.ToString());
                    Thread.Sleep(30000);
                }
            }
        }



        /// <summary>
        /// Consul 地址
        /// </summary>
        public static string ConsulAddress { get; set; }

        /// <summary>
        /// 网络连接状态
        /// </summary>
        public static bool NetworkConnectState { get; set; }

        /// <summary>
        /// 广告地址
        /// </summary>
        public static Uri SorbetesAddress { get; set; }

        /// <summary>
        /// 客户端ID
        /// </summary>
        public static int ClientId { get; set; }

        #region MQTT连接信息
        /// <summary>
        /// mqtt 服务地址
        /// </summary>
        public static string MqttAddress { get; set; }

        /// <summary>
        /// mqtt 用户名
        /// </summary>
        public static string MqttUserName { get; set; }

        /// <summary>
        /// mqtt 密码
        /// </summary>
        public static string MqttPassword { get; set; }

        /// <summary>
        /// mqtt 端口号
        /// </summary>
        public static int MqttPort { get; set; }
        #endregion

        #region API地址

        /// <summary>
        /// Api 网关地址
        /// </summary>
        public static string ApiAddress { get; set; }

        /// <summary>
        /// 订单服务
        /// </summary>
        public static string OrderServer { get; set; }

        /// <summary>
        /// 库存服务
        /// </summary>
        public static string StockServer { get; set; }
        #endregion

        #region IOT设备地址
        /// <summary>
        /// IOT ProductKey
        /// </summary>
        public static string ProductKey { get; set; }
        /// <summary>
        /// IOT DeviceName
        /// </summary>
        public static string DeviceName { get; set; }
        /// <summary>
        /// IOT DeviceSecret
        /// </summary>
        public static string DeviceSecret { get; set; }
        /// <summary>
        /// IOT DeviceMS
        /// </summary>
        public static string DeviceMS { get; set; }
        #endregion

    }
}
