using ProjectManagementSystem.Common.Logger;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.Common.Function
{
    public class AppSetting
    {
        private static string m_configurationFilePath;
        private static Configuration m_configuration;

        static AppSetting()
        {
            string s = System.Reflection.Assembly.GetExecutingAssembly().Location;
            System.IO.FileInfo finfo = new System.IO.FileInfo(s);
            string currPath = finfo.DirectoryName;

            m_configurationFilePath = System.IO.Path.Combine(currPath, "ProjectManagementSystem.exe");
            m_configuration = ConfigurationManager.OpenExeConfiguration(m_configurationFilePath);
        }

        public static string GetHostIp()
        {
            string key = "HostIp";
            string address = TryGetValue<string>(key);
            if(string.IsNullOrEmpty(address))
            {
                address = "localhost";
                //address = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName())
                //    .FirstOrDefault(d => d.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                //    .ToString();

                //if(string.IsNullOrEmpty(address))
                //{
                //    address = "localhost";
                //}

                if (!m_configuration.AppSettings.Settings.AllKeys.Contains(key))
                {
                    m_configuration.AppSettings.Settings.Add(new KeyValueConfigurationElement(key, address));
                    CLog.Instance.SystemLog.WriteDebug($"添加配置\"{key}\"={address}");
                }
                else
                {
                    m_configuration.AppSettings.Settings[key].Value = address;
                    CLog.Instance.SystemLog.WriteDebug($"更新配置\"{key}\"={address}");
                }
            }
            return address;
        }

        public static string GetCasunAgvUrl()
        {
            return $"{$"http://{GetHostIp()}:9528"}";
        }

        public static string GetAutoUpdateUrl()
        {
            return $"{$"http://{GetHostIp()}:63300/api/ver/pmsxml"}";
        }

        public static string GetPmsApiUrl(string apiName)
        {
            //return $"{GetString("PmsApi")}{apiName}";
            return $"{$"http://{GetHostIp()}:62200/api/Agv/"}{apiName}";
        }

        public static string GetPmsApiCustomUrl(string apiName)
        {
            //return $"{GetString("PmsApiCustom")}{apiName}";
            return $"{$"http://{GetHostIp()}:63300/api/Pms/"}{apiName}";
        }

        public static T TryGetValue<T>(string key)
        {
            T result;
            try
            {
                if (!m_configuration.AppSettings.Settings.AllKeys.Contains(key))
                {
                    result = default(T);
                    CLog.Instance.SystemLog.WriteDebug($"读取配置不包含 \"{key}\"， 返回默认值:{result}");
                    return result;
                }

                string value = m_configuration.AppSettings.Settings[key].Value;

                if (string.IsNullOrEmpty(value)) return default(T);
                return (T)Convert.ChangeType(value, typeof(T));

                //result = ((!string.IsNullOrEmpty(value)) ? parseFunc(value) : defaultTValueFunc());
            }
            catch (Exception ex)
            {
                CLog.Instance.SystemLog.WriteException("AppSetting", ex);
                result = default(T);
            }
            return result;
        }
    }
}
