package com.crm.login.rely.backend.configuration;

import com.crm.login.rely.backend.interceptor.CustomerInfoArgumentResolver;
import com.crm.login.rely.backend.interceptor.LoginInterceptor;
import com.crm.login.rely.backend.property.LoginConfigureProperty;
import com.crm.rely.backend.aspect.ControllerAop;
import com.crm.rely.backend.service.RedisService;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.ServletWebArgumentResolverAdapter;

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

/**
 * @author houn
 */
@Order(2)
public class InterceptorConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    LoginConfigureProperty loginConfigureProperty;

    @Autowired
    private RedisService redisService;
    @Value("${login.single-sign-on:false}")
    private boolean singleSignOn;

    @Autowired
   private ControllerAop controllerAop;

    private static String[] excludePath = new String[5];

    static {
        excludePath[0] = "/error";
        excludePath[1] = "/file/callback";
        excludePath[2] = "/test/test";
        excludePath[3] = "/country/getCodeByIp";
        excludePath[4] = "/";
    }

    @Bean
    LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        List<String> stringList = new ArrayList<String>();
        for (String value : excludePath) {
            stringList.add(value);
        }
        for (String value : loginConfigureProperty.getExcludePathPatterns()) {
            if (!Strings.isNullOrEmpty(value)) {
                stringList.add(value);
            }
        }
        String[] excludeList = new String[stringList.size()];

        stringList.toArray(excludeList);
        registry.addInterceptor(new LoginInterceptor(redisService,singleSignOn,controllerAop))
                .addPathPatterns("/*/**").excludePathPatterns(excludeList);

        super.addInterceptors(registry);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowCredentials(true).allowedMethods("POST").maxAge(3600);
        super.addCorsMappings(registry);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new ServletWebArgumentResolverAdapter(new CustomerInfoArgumentResolver(redisService, controllerAop)));
        super.addArgumentResolvers(argumentResolvers);
    }
}
