using APT.Infrastructure.Core;
using APT.Infrastructure.EF;
using APT.Infrastructure.Utility;
using Autofac;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Threading;
using APT.Infrastructure.Api;
namespace APT.Utility
{
    public static class BaseServiceRegister
    {
        public static void RegisterComponents(this ContainerBuilder builder)
        {
            var isSql = APT.Infrastructure.Api.ConfigurationManager.AppSettings["isSqlLog"];
            if (!string.IsNullOrEmpty(isSql) && bool.Parse(isSql))
            {
                builder.RegisterType<EFDbContextLoggerProvider>().As<ILoggerProvider>().InstancePerLifetimeScope();
                builder.RegisterType<LoggerFactory>().As<ILoggerFactory>().InstancePerLifetimeScope();
                builder.RegisterType<EfDbContext>().PropertiesAutowired();
            }
            builder.RegisterType<Repository>().As<IRepository>().InstancePerLifetimeScope();
            builder.RegisterType<CommonService>().As<ICommonService>().InstancePerLifetimeScope();

            builder.RegisterType<DomainServiceBase>().As<IDomainService>().InstancePerLifetimeScope();
            builder.RegisterType<AspNetSessionProvider>().As<ISessionProvider>().InstancePerLifetimeScope();
            builder.RegisterType<EfDbContext>().As<IUnitOfWork>()
                .PropertiesAutowired();
            builder.RegisterType<RedisManager>().SingleInstance();
            //   var builders = new ConfigurationBuilder()
            //.AddJsonFile("appsettings.json");
            //AppConfig.Configuration = builders.Build();
            //AppConfig.Configuration = ConfigurationManager.Configuration.GetSection("");
            // var csredis = new CSRedis.CSRedisClient(null, GetRedisConnects());
            //RedisHelper.Initialization(csredis);
            //var s = ConfigurationManager.InfluxDbOption;
            //var t = ConfigurationManager.RedisOption;
            //ConfigurationHelper.Configuration = builders.Build();
            //Console.WriteLine(ConfigurationHelper.InfluxDbOption.Url);

            //int minWorker, minIOC;
            //ThreadPool.GetMinThreads(out minWorker, out minIOC);
            //// Change the minimum number of worker threads to four, but
            //// keep the old setting for minimum asynchronous I/O 
            //// completion threads.
            //if (ThreadPool.SetMinThreads(300, minIOC))
            //{
            //    // The minimum number of threads was set successfully.
            //}
            //else
            //{
            //    // The minimum number of threads was not changed.
            //}
            //ThreadPool.GetMinThreads(out minWorker, out minIOC);
        }

        private static string[] GetRedisConnects()
        {
            var connectString = ConfigurationManager.RedisStrings["RedisConnectionString"];
            var connectStrings = connectString.Split(",");
            var connects = new List<string>();

            foreach (var con in connectStrings)
            {
                connects.Add($"{con},defaultDatabase=0,poolsize=100,writeBuffer=10240");
            }

            return connects.ToArray();
        }

    }
}
