using CSRedis;
using dodohold.core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using YunhuiKit;

namespace molilian.core
{
    public static class CustomToken
    {
        public static IServiceCollection Init(this IServiceCollection services)
        {
            //int minWorkerThreads = 128;  // 工作线程数的建议设置
            //int minIOCompletionThreads = 512;  // I/O 完成端口线程数的建议设置
            //ThreadPool.SetMinThreads(minWorkerThreads, minIOCompletionThreads);


            //int newMaxWorker = 65535;  // 新的最大工作线程数
            //int newMaxIOC = 2000;      // 新的最大I/O完成端口线程数
            //ThreadPool.SetMaxThreads(newMaxWorker, newMaxIOC);

            //注入验证 provider
            MyAuthorizationFactory.Register(new AdminProvider());
            MyAuthorizationFactory.Register(new AppProvider());
            MyAuthorizationFactory.Register(new APIProvider(), true);


            //Configuration = configuration;

            //AppUtility.Configuration = Configuration;
            //var appSettings = Configuration.GetSection("AppSettings");
            //appSettings.Bind(AppUtility.AppSettings);

            //定期任务
            TimerScheduler.TaskContainers taskContainers = new();
            TimerScheduler timerScheduler = new TimerScheduler(taskContainers);
            timerScheduler.Start();

            SystemMonitor.StartMonitoring(30);

            try
            {
                var connstr = Environment.GetEnvironmentVariable("RedisConfig") ?? throw new ArgumentNullException("找不到对应的RedisConfig配置！");
                RedisKit.InitializeAsync(connstr);

                //初始化Redis
                RedisUtility.Init();
            }
            catch (Exception ex)
            {
                var endPoint = Environment.GetEnvironmentVariable("EndPoint");
                string message = $"【{endPoint}系统异常】初始化Redis失败 \n{ex.Message}\n{ex.StackTrace}";
                NotifyCore.Notify(message);
            }


            //services.AddMvc(option =>
            //{
            //    option.Filters.Add(typeof(MyAuthorizationFactory.MyActionFilter));
            //    option.Filters.Add(typeof(ResponseExceptionFilterAttribute));
            //});
            return services;
        }

    }
}
