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();
            //注册数据库
            services.AddDbContext<SMDbContext>(option => {
                //option.UseSqlServer(this.Configuration.GetValue<string>("Database:connStr"));
                option.UseMySql(this.Configuration.GetValue<string>("Database:dbConnStr"));
            });
            //注册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 => {
                var option = this.Configuration.GetSection("SwaggerDoc");
                c.SwaggerDoc("V1",new OpenApiInfo {
                    Title = option.GetValue<string>("Title"),
                    Version = option.GetValue<string>("Version"),
                    Description = option.GetValue<string>("Description"),
                });
                var basePath = AppContext.BaseDirectory;
                c.IncludeXmlComments(Path.Combine(basePath,"StaffManagement.xml"),true);
                c.AddXmlEnumEnable(Path.Combine(basePath,"StaffManagement.xml"));
            });
            //注册api错误处理器
            services.AddScoped<ApiExceptionFilterAttribute>();
        }

        // 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();
            } else {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();

            app.UseSwagger();
            app.UseSwaggerUI(c => {
                c.SwaggerEndpoint("/swagger/V1/swagger.json","接口文档");
                //c.RoutePrefix = "";
                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?}");
            });
        }
    }
}
