using System.Reflection;

using FreeSql;
using FreeSql.DatabaseModel;

using Furion.DataEncryption;

using Newtonsoft.Json;

using PTCMS.API.Domain.Entities;
using PTCMS.API.Domain.Repositories;
using PTCMS.API.Infrastructure.Repositories;
using PTCMS.API.Infrastructure.Utils.Consts;
using PTCMS.API.Infrastructure.Utils.Helpers;

namespace PTCMS.API.Infrastructure.Utils.Extensions
{
    /// <summary>
    /// 服务扩展
    /// </summary>
    public static class ServiceExtensions
    {
        /// <summary>
        /// 注册freesql
        /// </summary>
        /// <param name="services"></param>
        public static void AddFreeSql(this IServiceCollection services)
        {
            var connStr = AESEncryption.Decrypt(AppSettingHelper.ReadString("DbConn:MySQL"), Keys.DbConnAesKey);

            IFreeSql fsql = new FreeSql.FreeSqlBuilder()
#if DEBUG
                    .UseNoneCommandParameter(true)
                    .UseAutoSyncStructure(true)
#else

                    .UseGenerateCommandParameterWithLambda(true)
#endif

                    .UseConnectionString(FreeSql.DataType.MySql, connStr)
                    .Build();

            fsql.Aop.CommandBefore += (s, e) =>
            {
                Console.WriteLine($"--------------Start:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}--------------");
                Console.WriteLine($"CommandBefore->监视线程:{Thread.CurrentThread.ManagedThreadId};");
                Console.WriteLine("CommandBefore-->Identifier：" + e.Identifier + "");
            };

            fsql.Aop.CommandAfter += (s, e) =>
            {
                Console.WriteLine($"CommandAfter->监视线程:{Thread.CurrentThread.ManagedThreadId};");
                Console.WriteLine("CommandAfter-->Identifier：" + e.Identifier + "");
                Console.WriteLine($"CommandAfter->监视SQL:{e.Command.CommandText}");
                Console.WriteLine("CommandAfter-->耗时：" + e.ElapsedMilliseconds + "：毫秒");
                Console.WriteLine($"--------------End:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}--------------");
            };

            fsql.Aop.CurdAfter += (s, e) =>
            {
                Console.WriteLine($"CurdAfter->监视线程:{Thread.CurrentThread.ManagedThreadId};");
                Console.WriteLine("Identifier：" + e.Identifier + "");
                Console.WriteLine($"CurdAfter->监视实体:{e.EntityType.Name}");
                Console.WriteLine($"CurdAfter->监视执行时间:{e.ElapsedMilliseconds}" + "：毫秒");
                Console.WriteLine($"CurdAfter->监视SQL:{e.Sql}");
                Console.WriteLine($"CurdAfter->监视参数：{string.Join(',', e.DbParms.AsEnumerable())}");
                Console.WriteLine($"--------------End:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}--------------");
            };


            //fsql.CodeFirst.CodeFirstExt();
            //var data = JsonConvert.SerializeObject(fsql.Select<Certificate>().ToList());
            services.AddFreeRepository(null, nameof(ServiceExtensions).GetType().Assembly);
            services.AddSingleton<IFreeSql>(fsql);
        }

        /// <summary>
        /// 注册自定义服务以及仓储
        /// </summary>
        /// <param name="services"></param>
        public static void AddCustomServices(this IServiceCollection services)
        {
            services.AddScoped<ICertificateRepository, CertificateRepository>();
        }
    }
}