using System;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using Falcon.Extend;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Redis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using StaffManagement.Database;
using StaffManagement.Extensions;

namespace StaffManagement
{
    public class Startup
    {
        public Startup(IConfiguration configuration) {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services) {
            //注册Json序列化
            services.AddMsJsonProvider();
            //注册数据库
            var dbStr = this.Configuration.GetValue<string>("Database:dbConnStr");
            services.AddDbContext<SMDbContext>(option => {
                option.UseMySql(dbStr);
            });
            //注册Redis
            var rop = this.Configuration.GetSection("Redis").Get<RedisCacheOptions>();
            services.AddRedis(rop);
            //注册缓冲帮助器
            services.AddSingleton<CacheHelper>();
            //注册MVC
            services.AddControllersWithViews()
                .AddJsonOptions(option => {
                    option.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
                    option.JsonSerializerOptions.PropertyNamingPolicy = null;
                });

            //注册Swagger
            services.AddSwaggerGen(c => {
                c.SwaggerDoc("V1",new OpenApiInfo {
                    Title = "员工管理API接口",// option.GetValue<string>("Title"),
                    Version = "1.0",
                    Description = "统一采用API接口设计，查看下面详细接口规范。",
                });
                var basePath = AppContext.BaseDirectory;
                c.IncludeXmlComments(Path.Combine(basePath,"StaffManagement.xml"),true);
                c.AddXmlEnumEnable(Path.Combine(basePath,"StaffManagement.xml"));
            });
            //注册api错误处理器
            services.AddScoped<ApiExceptionFilterAttribute>();
        }

        public void Configure(IApplicationBuilder app,IWebHostEnvironment env,SMDbContext db) {
            if(env.IsDevelopment()) {
                app.UseDeveloperExceptionPage();
            } else {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();
            db.Init();
            app.UseSwagger();
            app.UseSwaggerUI(c => {
                c.SwaggerEndpoint("/swagger/V1/swagger.json","接口文档");
                c.RoutePrefix = "api";
                c.DocumentTitle = "一站式医疗救助接口文档";
                c.InjectStylesheet("/css/swagger.css");
                c.InjectJavascript("/lib/jquery/dist/jquery.min.js");
                c.InjectJavascript("/js/swagger.js");
            });


            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints => {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
