package com.njzscloud.common.mvc.config;

import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 参数、返回值处理器配置
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
public class RequestMappingHandlerAdapterAutoConfiguration implements InitializingBean {

    private final RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    /**
     * 收集自定义的 Controller 方法参数处理器
     */
    private final ObjectProvider<HandlerMethodArgumentResolver> handlerMethodArgumentResolver;

    /**
     * 收集自定义的 Controller 方法返回值处理器
     */
    private final ObjectProvider<HandlerMethodReturnValueHandler> handlerMethodReturnValueHandler;

    public RequestMappingHandlerAdapterAutoConfiguration(@Autowired(required = false) RequestMappingHandlerAdapter requestMappingHandlerAdapter,
                                                         ObjectProvider<HandlerMethodArgumentResolver> handlerMethodArgumentResolver,
                                                         ObjectProvider<HandlerMethodReturnValueHandler> handlerMethodReturnValueHandler) {
        this.requestMappingHandlerAdapter = requestMappingHandlerAdapter;
        this.handlerMethodArgumentResolver = handlerMethodArgumentResolver;
        this.handlerMethodReturnValueHandler = handlerMethodReturnValueHandler;
    }

    @Override
    public void afterPropertiesSet() throws Exception {

        if (requestMappingHandlerAdapter == null) {
            log.warn("RequestMappingHandlerAdapter 为空");
            return;
        }

        List<HandlerMethodArgumentResolver> handlerMethodArgumentResolverList = handlerMethodArgumentResolver.orderedStream().collect(Collectors.toList());
        List<HandlerMethodReturnValueHandler> handlerMethodReturnValueHandlerList = handlerMethodReturnValueHandler.orderedStream().collect(Collectors.toList());

        // Controller 方法参数处理器
        if (CollUtil.isNotEmpty(handlerMethodArgumentResolverList)) {
            List<HandlerMethodArgumentResolver> argumentResolvers = requestMappingHandlerAdapter.getArgumentResolvers();
            if (argumentResolvers != null) {
                handlerMethodArgumentResolverList.addAll(argumentResolvers);
            }
            requestMappingHandlerAdapter.setArgumentResolvers(handlerMethodArgumentResolverList);
        }

        // Controller 方法返回值处理器
        if (CollUtil.isNotEmpty(handlerMethodReturnValueHandlerList)) {
            List<HandlerMethodReturnValueHandler> returnValueHandlers = requestMappingHandlerAdapter.getReturnValueHandlers();
            if (returnValueHandlers != null) {
                handlerMethodReturnValueHandlerList.addAll(returnValueHandlers);
            }
            requestMappingHandlerAdapter.setReturnValueHandlers(handlerMethodReturnValueHandlerList);
        }
    }
}
