using IP2Region.Net.Abstractions;
using IP2Region.Net.XDB;
using IRaCIS.Core.Application.BackGroundJob;
using IRaCIS.Core.Application.Helper;
using IRaCIS.Core.Application.Service;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infra.EFCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Panda.DynamicWebApi;
using System;
using System.IO;
using System.Linq;
using System.Reflection;

namespace IRaCIS.Core.API;


public static class ServiceCollectionSetup
{
    public static void ConfigureServices(this IServiceCollection services, IConfiguration _configuration)
    {
        services.AddOptions().Configure<SystemEmailSendConfig>(_configuration.GetSection("SystemEmailSendConfig"));
        services.AddOptions().Configure<ServiceVerifyConfigOption>(_configuration.GetSection("BasicSystemConfig"));
        services.AddOptions().Configure<AliyunOSSOptions>(_configuration.GetSection("AliyunOSS"));
        services.AddOptions().Configure<ObjectStoreServiceOptions>(_configuration.GetSection("ObjectStoreService"));
        services.AddOptions().Configure<IRCEncreptOption>(_configuration.GetSection("EncrypteResponseConfig"));
        services.AddOptions().Configure<RequestDuplicationOptions>(_configuration.GetSection("RequestDuplicationOptions"));
        services.AddOptions().Configure<SystemPacsConfig>(_configuration.GetSection("SystemPacsConfig"));
        services.Configure<IRaCISBasicConfigOption>(_configuration.GetSection("IRaCISBasicConfig"));

        services.Configure<ServiceVerifyConfigOption>(_configuration.GetSection("BasicSystemConfig"));

        //ת��ͷ����  ��ȡ��ʵIP 
        services.Configure<ForwardedHeadersOptions>(options =>
        {
            options.ForwardedHeaders =
                ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
        });

        services.AddSingleton<IUserIdProvider, IRaCISUserIdProvider>();
        services.AddSingleton<ISearcher>(new Searcher(CachePolicy.Content, Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources, "ip2region.xdb")));




        services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
        services.AddScoped<IRepository, Repository>();

        services.AddScoped<IObtainTaskAutoCancelJob, ObtainTaskAutoCancelJob>();

        // ע����Service ��β�ķ���
        services.Scan(scan => scan
            .FromAssemblies(typeof(BaseService).Assembly)
            .AddClasses(classes => classes.Where(t => t.Name.Contains("Service")))
            .AsImplementedInterfaces()
            .WithScopedLifetime());

        #region Register Controllers

        // Register all controllers in the assembly that implement IDynamicWebApi
        services.Scan(scan => scan
            .FromAssemblies(typeof(BaseService).Assembly)
            .AddClasses(classes => classes.AssignableTo<IDynamicWebApi>())
            .AsSelf()
            .WithScopedLifetime());

        #endregion


        // MediatR ��������Ϣ �¼�����  �ӳ����� ע�������handler��Ӧ��ϵ
        //builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<ConsistencyVerificationHandler>());



        #region  ��ʷ��������
        //builder.Services.AddMemoryCache();
        ////�ϴ����� ����
        //builder.Services.Configure<FormOptions>(options =>
        //{
        //    options.MultipartBodyLengthLimit = int.MaxValue;
        //    options.ValueCountLimit = int.MaxValue;
        //    options.ValueLengthLimit = int.MaxValue;
        //});
        //IP ���� �����ð����� ���ߺ�����
        //services.AddIpPolicyRateLimitSetup(_configuration);
        // �û����� ������Ȩ
        //services.AddAuthorizationPolicySetup(_configuration);
        #endregion
    }


}

#region Autofac ����
//public class AutofacModuleSetup : Autofac.Module
//{
//    protected override void Load(ContainerBuilder containerBuilder)
//    {

//        #region  byzhouhang  20210917  �˴�ע�᷺�Ͳִ� ���Լ���Domain�� ��Infra.EFcore ���� �յĲִ��ӿڶ���� �ִ��ļ�����

//        containerBuilder.RegisterGeneric(typeof(Repository<>))
//            .As(typeof(IRepository<>)).InstancePerLifetimeScope();//ע�᷺�Ͳִ�

//        containerBuilder.RegisterType<Repository>().As<IRepository>().InstancePerLifetimeScope();


//        #endregion

//        #region ָ��������Ҳ��autofac ������ʵ����ȡ https://www.cnblogs.com/xwhqwer/p/15320838.html

//        //��ȡ���п��������Ͳ�ʹ������ע��
//        containerBuilder.RegisterAssemblyTypes(typeof(BaseService).Assembly)
//            .Where(type => typeof(IDynamicWebApi).IsAssignableFrom(type))
//            .PropertiesAutowired();

//        #endregion


//        Assembly application = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "IRaCIS.Core.Application.dll");
//        containerBuilder.RegisterAssemblyTypes(application).Where(t => t.FullName.Contains("Service"))
//            .PropertiesAutowired().AsImplementedInterfaces();

//        //containerBuilder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();
//        //containerBuilder.RegisterType<UserInfo>().As<IUserInfo>().InstancePerLifetimeScope();


//        //ע��hangfire����  ����ע��
//        containerBuilder.RegisterType<ObtainTaskAutoCancelJob>().As<IObtainTaskAutoCancelJob>().InstancePerDependency();

//    }
//}
#endregion
