using Microsoft.Extensions.Configuration;
using System;

namespace Nirvana.Common
{
    public static class Configs
    {
        private static IConfiguration _configuration;

        static Configs()
        {
            //在当前目录或者根目录中寻找appsettings.json文件
            var fileName = "appsettings.json";
            var directory = AppContext.BaseDirectory;
            directory = directory.Replace("\\", "/");

            var filePath = $"{directory}/{fileName}";
            if (!System.IO.File.Exists(filePath))
            {
                var length = directory.IndexOf("/bin");
                filePath = $"{directory.Substring(0, length)}/{fileName}";
            }

            var builder = new ConfigurationBuilder()
                .AddJsonFile(filePath, false, true);

            _configuration = builder.Build();
        }
        public static string GetSectionValue(string key)
        {
            if (_configuration.GetSection(key) != null)
                return _configuration.GetSection(key).Value;
            else
                return "";
        }

        public static bool GetBoolValue(string key)
        {
            bool value = false;
            bool.TryParse(GetSectionValue(key), out value);
            return value;
        }

        public static int GetIntValue(string key)
        {
            int value = 0;
            int.TryParse(GetSectionValue(key), out value);
            return value;
        }

        public static string GetString(string key)
        {
            return GetSectionValue(key);
        }


    }
}
