package com.yvan.mvc;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yvan.platform.YvanUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

@Configuration
public class JztdMvcAutoConfigration extends WebMvcConfigurerAdapter {

    @Bean
    public ObjectMapper objectMapper() {
        return YvanUtil.objectMapper;
    }

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        return new MappingJackson2HttpMessageConverter(objectMapper());
    }

    /**
     * 默认输出 json
     */
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
    }

    /**
     * http 转换请求
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        // 拦截 http请求，转换为想要的数据
        // org.springframework.web.method.support.InvocableHandlerMethod:156 行
        argumentResolvers.add(new QueryParamResolver());
        argumentResolvers.add(new JsonBodyResolver());
        argumentResolvers.add(new PageDbResolver());
        argumentResolvers.add(new JsonWapperResolver());
        argumentResolvers.add(new ParameterParserResolver());
    }

    ///**
    // * 跨域支持
    // */
    //@Override
    //public void addCorsMappings(CorsRegistry registry) {
    //    registry.addMapping("/**").allowedOrigins("https://wms.jztd.con");
    //}

    //@Override
    //public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
    //    //见 org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor
    //    //super.addReturnValueHandlers(handlers);
    //    //handlers.add(0, new JwtHandlerProcessor());
    //}
}
