using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrazyStudio.Core.Common
{
    public class AppSettings
    {
        static IConfiguration? Configuration { get; set; }

        public AppSettings(string basePath)
        {
            if (Configuration is null)
            {
#if DEBUG
                Configuration = new ConfigurationBuilder()
                   .SetBasePath(basePath)
                   .Add(new JsonConfigurationSource { Path = "appsettings.Development.json", ReloadOnChange = true })
                   .Build();
#else
                Configuration = new ConfigurationBuilder()
                   .SetBasePath(basePath)
                   .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
                   .Build();
#endif
            }
        }

        /// <summary>
        /// 封装要操作的字符
        /// </summary>
        /// <param name="sections">节点配置</param>
        /// <returns></returns>
        public static string Option(params string[] sections)
        {
            try
            {
                if (sections.Any())
                {
                    return Configuration![string.Join(":", sections)]!;
                }
            }
            catch (Exception) { }

            return "";
        }
    }
}
