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

import com.crm.login.rely.backend.interceptor.LoginExcludeInterceptor;
import com.crm.login.rely.backend.interceptor.LoginInterceptor;
import com.crm.login.rely.backend.property.LoginConfigureProperty;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

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

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

    @Autowired
    LoginConfigureProperty loginConfigureProperty;

    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 LoginExcludeInterceptor())
                .addPathPatterns(excludeList);

        super.addInterceptors(registry);
    }

}
