using medical.insu.transfomer.Attributes;
using ReZero.SuperAPI;
using System;
using System.Linq;
using System.Reflection;
namespace medical.insu.transfomer
{
    /// 
    /// 定义不需要登录验证的API路径
    /// 
    public static class NoAuthPaths
    {
        /// 
        /// 医保交易接口
        /// 
        public const string MedicalInsuranceExecuteTransaction = "/api/200100/medicalinsurancecontroller/executetransaction";
        /// 
        /// 检查路径是否在免验证列表中
        /// 
        /// 请求路径
        /// true表示不需要验证
        public static bool IsNoAuthPath(string path)
        {
            path = path.ToLower();
            
            // 检查是否匹配医保交易接口
            if (path.Contains(MedicalInsuranceExecuteTransaction))
            {
                Console.WriteLine($"路径匹配医保交易接口: {path}");
                return true;
            }
            
            // 如果需要添加更多免验证路径,可以在这里扩展
            
            // 添加更多需要跳过验证的接口
            if (path.Contains("/api/200100/medicalinsurancecontroller/"))
            {
                Console.WriteLine($"所有医保接口跳过验证: {path}");
                return true;
            }
            
            return false;
        }
        
        /// 
        /// 通过特性检查是否需要验证
        /// 
        /// 接口上下文
        /// true表示不需要验证
        public static bool IsSkipAuthByAttribute(InterfaceContext context)
        {
            var path = context.HttpContext.Request.Path;
            Console.WriteLine($"检查路径是否需要验证: {path}");
            
            // 先检查路径
            if (IsNoAuthPath(path))
            {
                Console.WriteLine($"路径已跳过验证: {path}");
                return true;
            }
            
            try
            {
                // 检查具体接口信息
                if (context.InterfaceInfo?.DataModel?.MyMethodInfo != null)
                {
                    var methodInfo = context.InterfaceInfo.DataModel.MyMethodInfo;
                    var classFullName = methodInfo.MethodClassFullName;
                    var methodName = methodInfo.MethodName;
                    
                    Console.WriteLine($"检查接口信息: {classFullName}.{methodName}");
                    
                    if (!string.IsNullOrEmpty(classFullName) && !string.IsNullOrEmpty(methodName))
                    {
                        // 获取所有程序集
                        var assemblies = AppDomain.CurrentDomain.GetAssemblies();
                        
                        foreach (var assembly in assemblies)
                        {
                            try
                            {
                                // 查找类型
                                if (!assembly.FullName.Contains("medical") && 
                                    !assembly.FullName.Contains("SuperAPI"))
                                    continue;
                                
                                var type = assembly.GetType(classFullName);
                                if (type != null)
                                {
                                    Console.WriteLine($"找到类型: {type.FullName}");
                                    
                                    // 检查类是否有SkipAuthAttribute
                                    if (type.GetCustomAttributes(typeof(SkipAuthAttribute), true).Any())
                                    {
                                        Console.WriteLine($"类型有SkipAuthAttribute: {type.FullName}");
                                        return true;
                                    }
                                    
                                    // 查找方法
                                    var method = type.GetMethod(methodName);
                                    if (method != null)
                                    {
                                        Console.WriteLine($"找到方法: {method.Name}");
                                        
                                        // 检查方法是否有SkipAuthAttribute
                                        if (method.GetCustomAttributes(typeof(SkipAuthAttribute), true).Any())
                                        {
                                            Console.WriteLine($"方法有SkipAuthAttribute: {method.Name}");
                                            return true;
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine($"找不到方法: {methodName}");
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                // 忽略查找过程中的异常
                                Console.WriteLine($"查找程序集时发生异常: {assembly.FullName}, 错误: {ex.Message}");
                                continue;
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("接口信息为空");
                }
            }
            catch (Exception ex)
            {
                // 捕获任何异常,防止验证过程崩溃
                Console.WriteLine($"检查免验证特性时发生异常: {ex.Message}, 堆栈: {ex.StackTrace}");
            }
            
            Console.WriteLine($"需要验证: {path}");
            return false;
        }
    }
}