using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace NEIntelligentControl2.Service.WebSocket
{
    /// <summary>
    /// 链接管理
    /// </summary>
    public class UrlManager
    {
        private string servicePath;
        /// <summary>
        /// 后台服务地址
        /// </summary>
        public string ServicePath
        {
            get
            {
                if (servicePath == null)
                {
                    servicePath = GetConfigurationValue();
                }
                return servicePath;
            }
        }
        private string dataServicePath;
        /// <summary>
        /// 数据地址
        /// </summary>
        public string DataServicePath
        {
            get
            {
                if (dataServicePath == null)
                {
                    dataServicePath = GetConfigurationValue();
                }
                return dataServicePath;
            }
        }

        private string alarmPath;
        /// <summary>
        /// 报警地址
        /// </summary>
        public string AlarmPath
        {
            get
            {
                if (alarmPath == null)
                {
                    alarmPath = GetConfigurationValue();
                }
                return alarmPath;
            }
        }

        private string serviceSocketPath;
        /// <summary>
        /// 后台推送地址
        /// </summary>
        public string ServiceSocketPath
        {
            get
            {
                if (serviceSocketPath == null)
                {
                    serviceSocketPath = GetConfigurationValue();
                }
                return serviceSocketPath;
            }
        }

        private string adapterSocketPath;
        /// <summary>
        /// 数据推送地址
        /// </summary>
        public string AdapterSocketPath
        {
            get
            {
                if (adapterSocketPath == null)
                {
                    adapterSocketPath = GetConfigurationValue();
                }
                return adapterSocketPath;
            }
        }

        private int windturbineDetailsInterval = -777;
        /// <summary>
        /// 风机详情页弹窗时间间隔
        /// </summary>
        public int WindturbineDetailsInterval
        {
            get
            {
                if (windturbineDetailsInterval == -777)
                {
                    windturbineDetailsInterval = GetConfigurationValueInt();
                }
                return windturbineDetailsInterval;
            }
        }

        private int GetConfigurationValueInt([CallerMemberName] string name = null)
        {
            if (name == null) return 0;
            try
            {
                var v = ConfigurationManager.AppSettings[name];
                int.TryParse(v, out int d);
                return d;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"读取配置文件[{name}]出错！", ex);
                return 0;
            }
        }

        /// <summary>
        /// 获取配置文件值
        /// </summary>
        private string GetConfigurationValue([CallerMemberName] string name = null)
        {
            if (name == null) return "";
            try
            {
#if (DEBUG)
                name += "Debug";
#endif
                return ConfigurationManager.AppSettings[name];
            }
            catch (Exception ex)
            {
                Console.WriteLine($"读取配置文件[{name}]出错！", ex);
                return "";
            }
        }
    }
}
