using Logger;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Configuration;
using System.Xml;

namespace Tools
{
    public class AppConfigManage
    {
        public static Dictionary<string, string> ReadConfigList()
        {

            Dictionary<string, string> appSettingsDictionary = new Dictionary<string, string>();
            var appSettings = ConfigurationManager.AppSettings;
            foreach (var key in appSettings.AllKeys)
            {
                appSettingsDictionary.Add(key, appSettings[key]);
            }
            return appSettingsDictionary;
        }

        public static string ReadConfig(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }

        public static void DeleteConfig(string key)
        {
            // 获取配置文件路径
            string configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
            Configuration config = ConfigurationManager.OpenExeConfiguration(configFile);

            // 加载xml文件
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(configFile);

            // 获取appSettings节点
            XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
            if (xNode != null)
            {
                // 查找并删除指定的节点
                XmlNode toRemove = xNode.SelectSingleNode($"add[@key='{key}']");
                if (toRemove != null)
                {
                    xNode.RemoveChild(toRemove);
                    config.AppSettings.Settings.Remove(key);
                }
            }
            // 保存xml文档
            xDoc.Save(configFile);

            // 保存配置文件
            config.Save(ConfigurationSaveMode.Modified);
            // 刷新配置节，以便ConfigurationManager使用最新的配置
            ConfigurationManager.RefreshSection("appSettings");
        }

        public static void SaveConfig(string key, string value)
        {
            try
            {
                // 获取配置文件路径
                string configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
                Configuration config = ConfigurationManager.OpenExeConfiguration(configFile);
                KeyValueConfigurationElement setting = config.AppSettings.Settings[key];

                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(configFile);//加载xml文件

                XmlNode xNode;
                XmlElement xElem1;
                XmlElement xElem2;

                xNode = xDoc.SelectSingleNode("//appSettings");//获取指定的xml子节点
                xElem1 = (XmlElement)xNode.SelectSingleNode($"//add[@key='{key}']");//获取子节点中指定的子节点
                                                                                    //如果能获取到节点，就修改节点的value值
                if (xElem1 != null)
                {
                    xElem1.SetAttribute("value", value);//给节点中的value属性赋值(修改操作)
                }
                //如果不能获取到节点，就创建节点
                else
                {
                    xElem2 = xDoc.CreateElement("add");
                    xElem2.SetAttribute("key", key);
                    xElem2.SetAttribute("value", value);
                    xNode.AppendChild(xElem2);
                }

                if (setting != null)
                {
                    setting.Value = value;
                }
                else
                {
                    config.AppSettings.Settings.Add(key, value);
                }

                xDoc.Save(configFile);//保存xml文档

                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");

            }
            catch (Exception ex)
            {
                Log.FunError(ex, MethodBase.GetCurrentMethod().Name);
            }
        }
    }
}
