
using BPA.CAP.Settings;
using DotNetCore.CAP;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Threading.Tasks;

namespace BAP.CAP
{
    /// <summary>
    ///     CAP扩展
    /// </summary>
    public static class CapExtensions
    {

        public static IServiceCollection AddCapConfiguration(this IServiceCollection services, IConfiguration configuration, CapSettings capSettings=null)
        {

            RabbitmqSettings rabbitmqSettings = new RabbitmqSettings();
            rabbitmqSettings= JsonConvert.DeserializeObject<RabbitmqSettings>(configuration.GetSection("rabbitmq_config").Value);

            if(capSettings==null) capSettings = new CapSettings();
            var dbConfig= configuration.GetSection("sqlserver_config").Value;
            services.AddCap(x => {
                x.UseSqlServer(configuration.GetSection("sqlserver_config").Value);
                x.UseRabbitMQ(options =>
                {
                    options.HostName = rabbitmqSettings.HostName;// "10.2.1.21";
                    options.UserName = rabbitmqSettings.UserName;// "guest";
                    options.Password = rabbitmqSettings.Password;// "guest";
                });
                x.UseDashboard();
                x.FailedRetryCount = 5;
                x.FailedThresholdCallback+= capSettings.FailedThresholdCallback;
                x.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
            });
            services.AddSingleton(capSettings);
           // db.ScopedContext
            //, db =>
            //  {
            //      //(A)全局生效配置点
            //      //调试SQL事件，可以删掉
            //      db.Aop.OnLogExecuting = (sql, pars) =>
            //      {
            //          Console.WriteLine(sql);//输出sql,查看执行sql
            //      };
            //  }


            //            ApolloClientSettings metaServerSettings = new ApolloClientSettings();
            //            builder.Configuration.GetSection(nameof(ApolloClientSettings)).Bind(metaServerSettings);
            //            services.AddSingleton(metaServerSettings);
            //#pragma warning disable CS8601 // 引用类型赋值可能为 null。
            //#pragma warning disable CS8604 // 引用类型参数可能为 null。
            //            var apollo = builder.Configuration.AddApollo(new ApolloOptions
            //            {
            //                AppId = metaServerSettings.AppId,
            //                MetaServer = metaServerSettings.Host + ":" + metaServerSettings.Port,
            //                Env = metaServerSettings.Env,
            //            }).AddNamespace(metaServerSettings.CommonNameSpace).AddNamespace(metaServerSettings.ClientNameSpace);
            //#pragma warning restore CS8604 // 引用类型参数可能为 null。
            //#pragma warning restore CS8601 // 引用类型赋值可能为 null。
            return services;
        }


    }
}
