package com.yiqi.config;

//import com.alibaba.fastjson.serializer.SerializerFeature;
//import com.alibaba.fastjson.support.config.FastJsonConfig;
//import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

import com.yiqi.common.constant.UrlConstants;
import com.yiqi.interceptor.AuthInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Configuration
@Primary
public class WebConfiguration implements WebMvcConfigurer {

    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter() {
        return new StringHttpMessageConverter();
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(stringHttpMessageConverter());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        InterceptorRegistration authInterceptor = registry.addInterceptor(getAuthInterceptor());
        authInterceptor.addPathPatterns("/" + UrlConstants.wxappApi + "/**");
        authInterceptor.excludePathPatterns(getAuthExcludePathPatterns());
    }


    private ArrayList<String> getAuthExcludePathPatterns(){
        ArrayList<String> list = new ArrayList<>();
        String[] urls = {
                "/" + UrlConstants.wxappApi + "/wx/**",
                "/" + UrlConstants.wxappApi + "/user/login",
                "/" + UrlConstants.wxappApi + "/user/forgetPassword",
                "/" + UrlConstants.wxappApi + "/user/regist",
                "/" + UrlConstants.wxappApi + "/banner/**",
                "/" + UrlConstants.wxappApi + "/user/wxLogin",
                "/" + UrlConstants.wxappApi + "/document/info"
        };
        Collections.addAll(list,urls);
        return list;
    }

    @Bean
    public AuthInterceptor getAuthInterceptor(){
        return new AuthInterceptor();
    }

}
