using IRaCIS.Core.Application.BusinessFilter;
using IRaCIS.Core.Application.Service.BusinessFilter;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Panda.DynamicWebApi;
using System.Collections.Generic;

namespace IRaCIS.Core.API
{
    public static class WebApiSetup
    {
        //20210910 避免冗余的控制器层代码编写，仅仅包了一层前后台定义的格式  这里采用动态webAPi+IResultFilter 替代大部分情况
        public static void AddDynamicWebApiSetup(this IServiceCollection services)
        {
            //动态webApi     目前存在的唯一小坑是生成api上服务上的动态代理AOP失效    间接掉用不影响
            services.AddDynamicWebApi(dynamicWebApiOption =>
            {
                //默认是 api
                dynamicWebApiOption.DefaultApiPrefix = "";
                //首字母小写
                dynamicWebApiOption.GetRestFulActionName = (actionName) => char.ToLower(actionName[0]) + actionName.Substring(1);
                //删除 Service后缀
                dynamicWebApiOption.RemoveControllerPostfixes.Add("Service");

            });
        }

        public static void AddMasaMinimalAPiSetUp(this IServiceCollection services) 
        {
            services.AddMasaMinimalAPIs(options =>
            {
                options.Prefix = "";//自定义前缀 默认是api
                options.Version = ""; //默认是V1
                options.AutoAppendId = false; //路由是否自动附加参数Id  默认是true
                options.PluralizeServiceName = false; //服务名称是否启用复数

                //options.Assemblies = new List<Assembly>() { typeof(UserSiteSurveySubmitedEventConsumer).Assembly };

                options.GetPrefixes = new List<string> { "Get", "Select", "Find" };
                options.PostPrefixes = new List<string> { "Post", "Add", "Create", "List" };
                options.PutPrefixes = new List<string> { "Put", "Update" };
                options.DeletePrefixes = new List<string> { "Delete", "Remove" };

                options.RouteHandlerBuilder = t => {
                    t.RequireAuthorization()
                    .AddEndpointFilter<LimitUserRequestAuthorizationEndpointFilter>()
                    .AddEndpointFilter<TrialGlobalLimitEndpointFilter>()
                    //.AddEndpointFilter<ModelValidationEndpointFilter>()     
                    .AddEndpointFilter<UnifiedApiResultEndpointFilter>()
                    .WithGroupName("Institution").DisableAntiforgery();
                };
                options.MapHttpMethodsForUnmatched = new string[] { "Post" };
                options.DisableTrimMethodPrefix = true; //禁用去除方法前缀
                options.DisableAutoMapRoute = false;//可通过配置true禁用全局自动路由映射或者删除此配置以启用全局自动路由映射
            });
        }



    }
}
