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 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"]);

            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 string ConsulAddress { get; set; }

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

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

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

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

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

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

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

        #region API地址

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

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

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

    }
}
