using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Threading.Tasks;
using Mall.ThirdCore.Message;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Mall.WebApi
{
    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.Configure<Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions>(options =>
            //{
            //    options.AllowSynchronousIO = true;
            //});
            services.Configure<Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions>(x => x.AllowSynchronousIO = true)
                .Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);
            services.AddControllers();
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); //注入http上下文
                                                                                //注入 Quartz调度类
                                                                                //services.AddSingleton<QuartzHelper>();
                                                                                //services.AddTransient<TimingHelper>();      // 这里使用瞬时依赖注入
           // services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();//注册ISchedulerFactory的实例。
                                                                            //任务调度
                                                                            //services.AddMvc().AddNewtonsoftJson(options =>
                                                                            //{
                                                                            //    // 忽略循环引用
                                                                            //    options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                                                                            //    // 不使用驼峰
                                                                            //    options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();//json字符串大小写原样输出
                                                                            //    // 设置时间格式
                                                                            //    options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                                                                            //    // 如字段为null值，该字段不会返回到前端
                                                                            //    // options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                                                                            //    options.SerializerSettings.Converters.
                                                                            //});
                                                                            //services.AddSingleton<IJobFactory, IOCJobFactory>();
            services.AddMvc().AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
                options.JsonSerializerOptions.PropertyNamingPolicy = null;
            });

            services.AddCors(options => options.AddPolicy("AllowCors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(new[]
            { "http://www.test.com:8080", "http://www.test.com:8081", "http://localhost:63342","http://localhost:59823","http://www.test.com:8082","http://activity.oytour.com","https://activity.oytour.com", "http://crmyx.oytour.com:8080","http://crmyx.oytour.com","http://fcrmyx.oytour.com", "http://yx.oytour.com","http://testerp.oytour.com:8080"})));
            //services.AddCors(options =>
            //{
            //    options.AddPolicy("AllowCors",
            //    builder =>
            //    {
            //        builder.WithOrigins("http://www.test.com:8080")
            //                            .SetIsOriginAllowedToAllowWildcardSubdomains()
            //                            .AllowAnyHeader()
            //                            .AllowAnyMethod()
            //                            .AllowCredentials(); 
            //    });
            //});
        }

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

            app.UseHttpsRedirection();

            app.UseRouting();
            // 允许所有跨域，cors是在ConfigureServices方法中配置的跨域策略名称
            app.UseCors("AllowCors");

            app.UseAuthorization();

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

            System.WebHttpContext.HttpContext.Configure(app.ApplicationServices.GetRequiredService<Microsoft.AspNetCore.Http.IHttpContextAccessor>());
            //启动信息发送
            Task.Run(() => MessageCore.Init());

            //获取前面注入的Quartz调度类
            //var quartz = app.ApplicationServices.GetRequiredService<QuartzHelper>();
            //appLifetime.ApplicationStarted.Register(() =>
            //{
            //    quartz.Start().Wait(); //网站启动完成执行
            //});

            //appLifetime.ApplicationStopped.Register(() =>
            //{
            //    quartz.Stop();  //网站停止完成执行

            //});
            //app.UseStaticFiles(new StaticFileOptions
            //{
            //    FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(
            //    Path.Combine(Directory.GetCurrentDirectory(), "ExportFile")),
            //    RequestPath = "/ExportFile"
            //});
        }
    }
}
