﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Configuration;

namespace Bohrsoft.Utility
{
    public class ConfigHelper
    {
        /// <summary>
        /// 获取appsetting中设置的键值对string
        /// </summary>
        /// <param name="key">键名称</param>
        /// <returns>键值-string</returns>
        public static string GetValue(string key)
        {
            string value = string.Empty;
            if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
            {
                value = ConfigurationManager.AppSettings[key].Trim();
            }
            return value;
        }
        /// <summary>
        ///  获取appsetting中设置的键值对int
        /// </summary>
        /// <param name="key">键名称</param>
        /// <returns>键值-int</returns>
        public static int GetValueInt(string key)
        {
            int value;
            Int32.TryParse(GetValue(key), out value);
            return value;
        }
        /// <summary>
        ///  获取appsetting中设置的键值对bool
        /// </summary>
        /// <param name="key">键名称</param>
        /// <returns>键值-bool</returns>
        public static bool GetValueBool(string key)
        {
            bool value;
            Boolean.TryParse(GetValue(key), out value);
            return value;
        }
        /// <summary>
        ///  获取appsetting中设置的键值对datetime
        /// </summary>
        /// <param name="key">键名称</param>
        /// <returns>键值-datetime</returns>
        public static DateTime GetValueDateTime(string key)
        {
            DateTime value;
            DateTime.TryParse(GetValue(key), out value);
            return value;
        }
        /// <summary>
        /// 根据当前应行的环境（web,exe)获取对应的config实例，
        /// </summary>
        /// <returns>返回config实例</returns>
        private static Configuration GetConfigClass()
        {
            Configuration config;
            if (HttpContext.Current != null && !HttpContext.Current.Request.PhysicalPath.Equals(string.Empty))
                config = WebConfigurationManager.OpenWebConfiguration("~");
            else
                config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            return config;
        }
        /// <summary>
        /// 设置appsetting的值，有则修改，无则添加
        /// </summary>
        /// <param name="key">键名称</param>
        /// <param name="value">键值</param>
        /// <param name="updateConfig">是否更新对应文件</param>
        public static void SetValue(string key, string value,bool updateConfig=true)
        {
            Configuration config = GetConfigClass();
            if (config.AppSettings.Settings.AllKeys.Contains(key))
            {
                config.AppSettings.Settings[key].Value = value;
            }
            else
            {
                config.AppSettings.Settings.Add(key, value);
            }
            if (updateConfig)
            {
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
            }
        }
        /// <summary>
        /// 删除appsetting的键值对
        /// </summary>
        /// <param name="key">键名称</param>
        /// <param name="updateConfig">是否更新对应文件</param>
        public static void DeleteKey(string key, bool updateConfig = true)
        {
            Configuration config = GetConfigClass();

            if (config.AppSettings.Settings.AllKeys.Contains(key))
            {
                config.AppSettings.Settings.Remove(key);
                if (updateConfig)
                {
                    config.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("appSettings");
                }
            }
           
        }
        /// <summary>
        /// 获取数据库连接节点
        /// </summary>
        /// <param name="key">节点名称</param>
        /// <returns>节点实例</returns>
        public static ConnectionStringSettings GetConnectionStringSettings(string key)
        {
            return ConfigurationManager.ConnectionStrings[key];
        }
        /// <summary>
        /// 获取数据库连接字符串
        /// </summary>
        /// <param name="key">节点名称</param>
        /// <returns>连接字符串</returns>
        public static string GetConnectionString(string key)
        {
            string connectionString = string.Empty;
            if (ConfigurationManager.ConnectionStrings[key] != null)
            {
                connectionString = ConfigurationManager.ConnectionStrings[key].ConnectionString;
            }
            return connectionString;
        }
    }
}
