using Hncore.Infrastructure.Service;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.Oss.Map;
using Hncore.Pass.OSS.Domain;
using Hncore.Pass.OSS.Utils;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using UEditor.Core;

namespace Hncore.Pass.OSS
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IHostingEnvironment env)
        {
            Configuration = env.UseAppsettings();
        }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<CourseContext>(opt => { opt.UseMySql(Configuration["MySql"]); });
            services.AddUEditorService();
            services.AddHttpClient();
            services.AutoAddService();
            services.AddAliOssClient(Configuration["Aliyun:Oss:AliEndpoint"], Configuration["Aliyun:Oss:AliAccessId"], Configuration["Aliyun:Oss:AliAccessKey"]);
         
            return services.Init(Configuration, CompatibilityVersion.Version_2_2, new ServiceOption
            {
                UseGlobalManageAuthFilter = false,
            });
        }

        public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime,
            ILoggerFactory loggerFactory)
        {
            app.UseStaticFiles();
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "upload")),
                RequestPath = "/upload",
                OnPrepareResponse = ctx =>
                {
                    ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=36000");
                }
            });

            app.Init(loggerFactory, applicationLifetime);
            MapConfig.Config();
        }
    }
}
