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 Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XYY.Authentication.Standard;
using XYY.Core.Standard.Data.Infrastructure;
using XYY.Service.Standard.RegionService;

namespace XYY.Api.Finance.Charges
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        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)
        {

            //r-wz9o30bx758o8jhbra.redis.rds.aliyuncs.com
            //r-wz9o30bx758o8jhbrapd.redis.rds.aliyuncs.com
            //不注册默认服务（因为服务实现的生命周期不同）
            //调试时使用公网地址
            //正式发布时使用内网地址
            services.AddXYYService(new ServiceOption
            {
                SqlServiceConnection = Configuration[DefaultConfig.SqlServiceConnectionKey],
                UseFinanceChargesRedis = true,
                FinanceChargesRedisConnection = "r-wz9o30bx758o8jhbrapd.redis.rds.aliyuncs.com",
                USEDBTransferAsMVC = false,
                UseRabbit=true
            }, Configuration);
            services.AddMvcCore(x =>
            {
                x.Filters.Add<ApiExceptionAcitonFilterAttribute>();
            });
            services.AddCors(option => option.AddPolicy(MyAllowSpecificOrigins, policy =>
        policy.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod()));
            services.AddControllers().AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();

            });

            services.AddAuthentication(BasicAuthenticationScheme.DefaultScheme)
                .AddScheme<BasicAuthenticationOption, BasicAuthenticationHandler>(BasicAuthenticationScheme.DefaultScheme, null
                );

            services.AddSingleton<Service.Standard.Finance.Charging.Attached.ITAttachedZipSet>();
        }


        // 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();
            });

        }
    }
}
