package com.persagy.dmp.auth.config;

import com.persagy.dmp.auth.client.EmsSaasWebFallbackFactory;
import com.persagy.dmp.auth.handler.AppContextHandler;
import com.persagy.dmp.auth.service.AuthService;
import com.persagy.dmp.auth.service.impl.BossAuthServiceImpl;
import com.persagy.dmp.auth.service.impl.EmsAuthServiceImpl;
import com.persagy.dmp.auth.service.impl.NoneAuthServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 通用请求拦截
 * @author Charlie Yu
 * @date 2021-11-02
 **/
@Configuration
@Order(1)
public class CommonWebConfigurer implements WebMvcConfigurer {

    @Bean
    public AppContextHandler appContextHandler() {
        return new AppContextHandler();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] includePaths = {"/**"};
        String[] excludePaths = {"/**/*.html", "/**/*.js", "/**/*.css", "/swagger-resources"};
        // 设置拦截的路径、不拦截的路径、优先级等等
        registry.addInterceptor(appContextHandler()).order(10).addPathPatterns(includePaths).excludePathPatterns(excludePaths);
    }

    @Bean
    @ConditionalOnProperty(value = "persagy.common.auth.channel", havingValue = "1")
    public EmsSaasWebFallbackFactory emsSaasWebFallbackFactory(){
        return new EmsSaasWebFallbackFactory();
    }

    @Bean
    @ConditionalOnProperty(value = "persagy.common.auth.channel", havingValue = "1")
    public AuthService emsAuthServiceImpl(){
        return new EmsAuthServiceImpl();
    }

    @Bean
    @ConditionalOnProperty(value = "persagy.common.auth.channel", havingValue = "-1",matchIfMissing = true)
    public AuthService noneAuthServiceImpl(){
        return new NoneAuthServiceImpl();
    }

    @Bean
    @ConditionalOnProperty(value = "persagy.common.auth.channel", havingValue = "2")
    public AuthService bossAuthServiceImpl(){
        return new BossAuthServiceImpl();
    }

}
