using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SqlSugar;
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.Sqlsugar
{
    /// <summary>
    ///     CAP扩展
    /// </summary>
    public static class CapExtensions
    {

        public static IServiceCollection AddCapConfiguration(this IServiceCollection services, IConfiguration configuration)
        {

            var dbConfig= configuration.GetSection("ConnectionStrings").Value;
           // };
            services.AddCap(x => {
                x.UseSqlServer(dbConfig);
                x.UseRabbitMQ(options =>
                {
                    options.HostName = "10.2.1.21";
                    options.UserName = "guest";
                    options.Password = "guest";
                });
                x.UseDashboard();
                x.FailedRetryCount = 5;
                x.FailedThresholdCallback = failed =>
                {
                    //var logger = failed.ServiceProvider.GetService<ILogger<Startup>>();
                    //logger.LogError($@"A message of type {failed.MessageType} failed after executing {x.FailedRetryCount} several times, 
                    //    requiring manual troubleshooting. Message name: {failed.Message.GetName()}");
                };
                x.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
            });

           // 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;
        }


    }
}
