/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.api.config;

import com.api.converter.CustomMessageConverter;
import com.api.form.ChildConvertForm;
import com.common.interceptor.AuthorizationInterceptor;
import com.api.resolver.LoginUserHandlerMethodArgumentResolver;
import com.common.interceptor.TestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * MVC配置
 *
 * @author Mark sunlightcs@gmail.com
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;
    @Autowired
    private LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authorizationInterceptor).addPathPatterns("/api/**");
        registry.addInterceptor(new TestInterceptor()).addPathPatterns("/interceptor/**");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(loginUserHandlerMethodArgumentResolver);
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new Converter<String, ChildConvertForm>() {
            @Override
            public ChildConvertForm convert(String s) {
                if(!s.isEmpty()) {
                    String[] split = s.split(",");
                    ChildConvertForm childConvertForm = new ChildConvertForm();
                    childConvertForm.setChildName(split[0]);
                    childConvertForm.setChildAge(Integer.parseInt(split[1]));
                    return childConvertForm;
                }
                return null;
            }
        });
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new CustomMessageConverter());
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        HashMap<String, MediaType> stringMediaTypeHashMap = new HashMap<>();
        stringMediaTypeHashMap.put("json",MediaType.APPLICATION_JSON);
        stringMediaTypeHashMap.put("xml",MediaType.APPLICATION_XML);
        stringMediaTypeHashMap.put("zxy",MediaType.parseMediaType("application/x-zxy"));
        ParameterContentNegotiationStrategy parameterContentNegotiationStrategy =
                new ParameterContentNegotiationStrategy(stringMediaTypeHashMap);
        parameterContentNegotiationStrategy.setParameterName("dataFormat");
        configurer.strategies(Arrays.asList(parameterContentNegotiationStrategy));
    }
}