using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using XYY.Authentication.Standard;
using XYY.Core.Standard.AliYun;
using XYY.Core.Standard.Data.Infrastructure;
using XYY.Core.SwaggerGen.Strandard;
using XYY.Service.Standard.KnowledgeBase;
using XYY.Service.Standard.KnowledgeBase.Data;
using XYY.Service.Standard.RegionService;
using XYY.Service.Standard.UserService;
namespace KnowledgeBase
{
    public class Startup
    {
        readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";//Ãû×ÖËæ±ãÆð
        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.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
                );
            //°¢ÀïÔÆ
            services.AddSingleton<IAliYunPostFileSerivce>(x => new AliYunPostFileSerivce(new  AliYunConfig
            {
                AliYunBucketName = Configuration[DefaultConfig.AliYunBucketName],
                AliYunEndPoint = Configuration[DefaultConfig.AliYunEndPoint],
                AliYunKeyId = Configuration[DefaultConfig.AliYunKeyId],
                AliYunKeySecret = Configuration[DefaultConfig.AliYunKeySecret],
                BasePath = Configuration[DefaultConfig.AliYunBasePath],
                CDNEndPoint = Configuration[DefaultConfig.AliYunPubEndPoint],
            })); 
            services.AddLogging();
            services.AddDB(Configuration["DBConnectionStrings:SqlService"]);
            services.AddScoped<IKnowledgeBaseRepository, KnowledgeBaseRepository>();
            services.AddScoped<IKnowledgeSevice, KnowledgeSevice>();
            services.RegionUserService();
            services.AddDistributedMemoryCache();
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SetAuth();
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "KnowledgeBase", Version = "v1" });
            });
        }

        // 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.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "KnowledgeBase v1"));
            }

            app.UseRouting();

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