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

namespace CrazyStudio.Core.Common.Tools.Extensions
{
    public static class ConfigurationExtension
    {
        /// <summary>
        /// 绑定扫描目录下JSON配置文件的信息
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        public static void AddConfigureFiles(this ConfigurationManager configuration)
        {
            var scanDirectories = configuration.GetSection("ConfigurationScanDirectories").Get<string[]>()!;
            foreach (var dir in scanDirectories)
            {
                if (string.IsNullOrWhiteSpace(dir))
                {
                    continue;
                }
                var fullPath = Path.Combine(Directory.GetCurrentDirectory(), dir);
                if (Directory.Exists(fullPath))
                {
                    var jsonFiles = Directory.GetFiles(fullPath);
                    foreach (var file in jsonFiles)
                    {
                        configuration
                        .AddJsonFile(file, optional: true, reloadOnChange: true);
                    }
                }
            }
        }
    }
}
