using DS.WMS.PrintApi.Middleware;
using DS.WMS.PrintApi.Service;
using DS.WMS.PrintApi.Utils;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using System.Text.Encodings.Web;
using System.Text.Unicode;

namespace DS.WMS.PrintApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddSqlSugarSetup();
            //services.AddSaasDbInstall();
            services.AddSingleton<ISaasDbService, SaasDbService>();
            services.AddSingleton<IOpenPrintService, OpenPrintService>();

            services.AddControllers(
                    (options =>
                    {
                        // 204Ԥ������
                        //options.Filters.Add<FegCrosFilter>();
                        //ȫ���쳣����
                        options.Filters.Add<GlobalExceptionsFilter>();
                        options.SuppressAsyncSuffixInActionNames = false;
                    }))
                .AddNewtonsoftJson(options =>
                {
                    //CustomContractResolver �������Զ���Ľ�����
                    options.SerializerSettings.ContractResolver = new CustomContractResolver();

                    // ʱ���ʽ��
                    options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                    // ����ѭ������
                    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                    // ���Կ�ֵ
                    // options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                })
                .AddJsonOptions(cfg =>
                {
                    //�����˷����������ı�����
                    cfg.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
                });
            //Swagger
            services.AddSwaggerSetup();

            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            var documentName = AppSetting.Configuration["SwaggerDoc:ContactName"];
            // app.UseHttpsRedirection();
            app.UseSwagger().UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", documentName);
            });
            app.UseRouting();
            //��̬�ļ�
            app.UseStaticFiles();
            // �ȿ�����֤
            app.UseAuthentication();
            // Ȼ������Ȩ�м��
            app.UseAuthorization();

            //app.UseMiddleware<ApiAuthMiddleware>();


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