package org.springblade.common.auth.config;

import org.springblade.common.auth.permission.CasSecurityInterceptor;
import org.springblade.common.auth.permission.PermissionHandler;
import org.springblade.common.auth.permission.PermissionInterceptor;
import org.springblade.common.auth.security.AccessTokenInterceptor;
import org.springblade.common.auth.security.XssParamSecurityFilter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.common.cache.CachesService;
import org.springblade.common.cache.support.JedisService;
import org.springblade.core.redis.cache.BladeRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class PermissionWebConfig extends WebMvcConfigurerAdapter {

    private static final Logger logger = LoggerFactory.getLogger(PermissionWebConfig.class);

    @Autowired
    private ApplicationContext context;

    @Autowired
    private BladeRedis redis;



    @Autowired
    private AccessTokenInterceptor accessTokenInterceptor;

    @Value("${cas.support.flag:false}")
    @Setter
    private boolean casSupportFlag;

    @Value("${spring.application.name}")
    @Setter
    private String appName;

    @Bean
    public PermissionInterceptor permissionInterceptor(){
        return new PermissionInterceptor(redis);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (appName != null && appName.equalsIgnoreCase("gateway-service")) {
            logger.info("Interceptors will be ignored for gateway service.");
            return;
        }
        if (casSupportFlag) {
            CasSecurityInterceptor casSecurityInterceptor = context
                    .getBean("casSecurityInterceptor", CasSecurityInterceptor.class);
            registry.addInterceptor(casSecurityInterceptor)
                    .excludePathPatterns("/app/report/**")
                    .excludePathPatterns("/app/waybill/carPush")
                    .excludePathPatterns("/app/smart/pay/notify")
                    .excludePathPatterns("/app/smart/pay/sendSms");
        }
        registry.addInterceptor(permissionInterceptor())
                .excludePathPatterns("/app/report/**")
                .excludePathPatterns("/app/waybill/carPush")
                .excludePathPatterns("/app/smart/pay/notify")
                .excludePathPatterns("/app/smart/pay/sendSms");
        registry.addInterceptor(accessTokenInterceptor)
                .excludePathPatterns("/app/report/**")
                .excludePathPatterns("/app/waybill/carPush")
                .excludePathPatterns("/app/smart/pay/notify")
                .excludePathPatterns("/app/smart/pay/sendSms");
    }

    @Bean
    public XssParamSecurityFilter securityFilter() {
        return new XssParamSecurityFilter();
    }











}