package cn.reghao.tnb.auth.app.config.web;

import org.apache.tomcat.util.http.Rfc6265CookieProcessor;
import org.apache.tomcat.util.http.SameSiteCookies;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author reghao
 * @date 2021-11-14 14:31:02
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    private final AccountInterceptor accountInterceptor;

    public WebConfig(AccountInterceptor accountInterceptor) {
        this.accountInterceptor = accountInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(accountInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/dist/**", "/theme/**", "/store/**", "/static/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/dist/**")
                .addResourceLocations("classpath:/static/dist/");
        registry.addResourceHandler("/*/dist/**")
                .addResourceLocations("classpath:/templates/");
    }

    //@Bean
    public TomcatContextCustomizer sameSiteCookiesConfig() {
        return context -> {
            final Rfc6265CookieProcessor cookieProcessor = new Rfc6265CookieProcessor();
            // 设置 Cookie 的 SameSite
            cookieProcessor.setSameSiteCookies(SameSiteCookies.NONE.getValue());
            context.setCookieProcessor(cookieProcessor);
        };
    }

    /**
     * 使用 CorsFilter 全局处理跨域
     *
     * @param
     * @return
     * @date 2023-02-14 09:42:06
     */
    /*@Bean
    public FilterRegistrationBean<CorsFilter> corsFilter() {
        CorsFilter corsFilter = new CorsFilter();
        FilterRegistrationBean<CorsFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(corsFilter);
        // 放在 FilterChainProxy 前面
        filterRegistrationBean.setOrder(-200);
        filterRegistrationBean.setName("corsFilter");
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }*/

    /**
     * 跨域处理
     *
     * @param
     * @return
     * @date 2023-02-11 04:18:11
     */
    /*@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }*/

    /**
     * 自定义 spring session 的 cookie, 默认是 JSESSIONID
     *
     * @param
     * @return
     * @date 2023-08-02 09:08:08
     */
    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        serializer.setCookieName("SESSDATA");
        serializer.setCookiePath("/");
        // domain 设置为一级域名
        serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
        //serializer.setCookieMaxAge(3600*24*30);
        return serializer;
    }
}
