using Microsoft.OpenApi.Models;
using SlnMesnac.Config;
using SlnMesnac.Quartz;
using SlnMesnac.Serilog;
using SlnMesnac.Extensions;
using System.Runtime.Serialization;

namespace SlnMesnac
{
    /// <summary>
    /// 
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        /// <summary>
        /// 
        /// </summary>
        public IConfiguration Configuration { get; }

        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services"></param>
        [Obsolete]
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            //配置Swagger
            services.AddSwaggerGen(swagger =>
            {
                //自定义接口信息
                swagger.SwaggerDoc("V1.0", new OpenApiInfo
                {
                    Title = "MES Web Api",
                    Version = "V1.0",
                    Description = $"API版本V1.0",
                    Contact = new OpenApiContact
                    {
                        Name = "MES Web Api",
                        Email = "wenjy@mesnac.com"
                    }
                });

                //自定义实体别名
                swagger.CustomSchemaIds(type => type.GetCustomAttributes(typeof(DataContractAttribute), true)
                                   .Cast<DataContractAttribute>()
                                   .FirstOrDefault()?.Name);

                //配置Action名称
                var path = Path.Combine(AppContext.BaseDirectory, "SlnMesnac.xml");
                swagger.IncludeXmlComments(path, true); // true : 显示控制器层注释
                swagger.OrderActionsBy(o => o.RelativePath); // 对action的名称进行排序，如果有多个，就可以看见效果了。
            });

            //注册配置类
            services.AddSingleton<AppConfig>(provider =>
            {
                var configuration = provider.GetService<IConfiguration>();
                return configuration.GetSection("AppConfig").Get<AppConfig>();

            });


            //注册SqlSugar
            services.AddSqlSugarSetup();

            //注册服务
            //services.AddServices();

            //注册任务调度
            services.AddQuartzSetUp();

        }

        /// <summary>
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //启用Swagger中间件
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/V1.0/swagger.json", "MES Web Api V1.0");
                c.RoutePrefix = "";
            });

            //启用Serilog中间件
            app.UseSerilogExtensions();

            //app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

        }
    }
}
