package com.ema.admin.handler;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import org.dromara.x.file.storage.spring.SpringFileStorageProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

/**
 * WebMvc 是配置静态资源访问、拦截器、权限验证处理器
 */
@Configuration
public class WebMvcHandler implements WebMvcConfigurer {

    @Resource
    private SpringFileStorageProperties springFileStorageProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，定义详细认证规则 https://sa-token.cc/doc.html
        registry.addInterceptor(new SaInterceptor(handler -> {
            // 指定一条 match 规则
            SaRouter.match("/**")    // 拦截的 path 列表，可以写多个
                    .notMatch("*.html", "*.css", "*.js", "/swagger**/**", "/webjars/**", "/v3/**", "/doc.html", "/csrf", "/error")
                    .notMatch("/queue.html", "/queue/**") // 排除队列监控页面
                    .notMatch("/static/**") // 排除掉静态资源
                    .notMatch("/login", "/logout", "/register", "/sendEmailCode", "/imgCode/**") // 排除登录、注册、发送邮件验证码接口
                    .notMatch("/ws/**") // 排除 WebSocket 接口
                    .notMatch( "/marketing/callback/**") // 排除极光回调接口
                    .notMatch("/api/websocket/**") // 排除 WebSocket API 文档接口
                    .notMatch("/v2/api-docs", "/swagger-resources/**", "/swagger-ui.html") // 排除 Knife4j 接口
                    .check(r -> StpUtil.isLogin()); // 要执行的校验动作，可以写完整的 lambda 表达式
        })).addPathPatterns("/**");
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置文件存储本地上传资源访问
        configureFileStorageResourceHandlers(registry);

        // 配置其他静态资源访问
        registry.addResourceHandler("/ema/**").addResourceLocations("classpath:/web/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/queue.html", "/redismq/**").addResourceLocations("classpath:/redismq/");
    }

    /**
     * 配置文件存储资源处理器
     */
    private void configureFileStorageResourceHandlers(ResourceHandlerRegistry registry) {
        List<? extends SpringFileStorageProperties.SpringLocalPlusConfig> localPlusList =
                springFileStorageProperties.getLocalPlus();
        if (CollUtil.isEmpty(localPlusList)) {
            return;
        }
        // 只遍历一次启用存储的配置
        for (SpringFileStorageProperties.SpringLocalPlusConfig config : localPlusList) {
            if (config.getEnableStorage()) {
                registry.addResourceHandler(config.getPathPatterns())
                        .addResourceLocations("file:/");
            }
        }
    }
}


