using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using XYY.Service.Standard.RegionService;
using XYY.Authentication.Standard;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using XYY.Service.Standard.First.DB;
using XYY.Common.Standard;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.CodeAnalysis;
using System.Threading;

namespace XYY.Api.First
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";//Ãû×ÖËæ±ãÆð


        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IFirstDB>(x => new ESFirstDB(Configuration[DefaultConfig.ESUrlKey]));
            services.AddSingleton<IFirstDB2>(x => new ESFirstDB2(Configuration[DefaultConfig.ESUrlKey]));
            services.AddSingleton<ITrackingLogDB>(x => new TrackingLogDB(Configuration[DefaultConfig.ESUrlKey]));
            services.AddXYYService(new ServiceOption
            {
                RedisConnection = "r-wz9o30bx758o8jhbrapd.redis.rds.aliyuncs.com",
                PBESConnection = Configuration[DefaultConfig.ESUrlKey],
                USEDBTransferAsMVC = true,
                CacheType = DistributedCacheType.Memory,
                SqlServiceConnection = Configuration[DefaultConfig.SqlServiceConnectionKey],
                UseRabbit = true,
                UseTrackingPUSHRedis = true,
            }, Configuration);
            services.AddSingleton<IWebHook>(x => new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580"));



            services.AddCors(option => option.AddPolicy(MyAllowSpecificOrigins, policy =>
            policy.AllowAnyHeader().AllowAnyOrigin().WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS")));
            services.AddControllers().AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
            });
            services.AddAuthentication(BasicAuthenticationScheme.DefaultScheme)
                .AddScheme<BasicAuthenticationOption, BasicAuthenticationHandler>(BasicAuthenticationScheme.DefaultScheme, null
                );
        }


        // 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.UseStatusCodePages();
            app.UseRouting();
            app.UseCors(MyAllowSpecificOrigins);
            app.UseBasicAuthentication();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
