package me.qwq.doghouse.config;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
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 org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;

import me.qwq.doghouse.interceptions.BlogInterceptor;
import me.qwq.doghouse.interceptions.ThemeStaticFilter;
import me.qwq.doghouse.service.ConfigService;

/**
 * Doghouse 配置项
 * <p>
 * 指定 RedisCacheManager 序列化器后需要重新指定启用 CacheProperties 的自动装配
 * @author Doghole
 *
 */
@Configuration
@EnableConfigurationProperties({DoghouseProperties.class, CacheProperties.class})
@Import(ConfigAutoRegistrar.class)
public class DoghouseConfig implements WebMvcConfigurer {
    
    @Autowired
    BlogInterceptor blogInterceptor;
    
    @Autowired
    DoghouseProperties doghouseProperties;
    
    @Autowired
    ConfigService configService;

    /**
     * 配置项注入拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(blogInterceptor)
                .addPathPatterns("/**");
    }

    /**
     * 上传文件资源映射、外部主题静态资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
        .addResourceHandler(doghouseProperties.getFileSqlPath() + "**")
        .addResourceLocations("file:" + doghouseProperties.getFileUploadPath());

        // URL: /themes/bleaching/css/main.css
        // FS:  workPath/themes/bleaching/static/css/main.css
        registry
        .addResourceHandler("/themes/**")
        .addResourceLocations("file:" + doghouseProperties.getWorkPath() + "/themes/");
        
        registry
        .addResourceHandler("/tmp/themes/**")
        .addResourceLocations("file:" + doghouseProperties.getWorkPath() + "/tmp/themes/");
    }
    
    @Bean
    public FilterRegistrationBean<ThemeStaticFilter> themeStaticFilter() {
        FilterRegistrationBean<ThemeStaticFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new ThemeStaticFilter());
        bean.addUrlPatterns("/themes/*");
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return bean;
    }
    
    @Bean
    public ITemplateResolver fileTemplateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setResolvablePatterns(Set.of("file:*"));
        resolver.setPrefix("");                      // 不要自动加前缀
        resolver.setSuffix(".html");                 // 自动补 .html
        resolver.setTemplateMode("HTML");
        resolver.setCharacterEncoding("UTF-8");
        resolver.setOrder(1);                        // 比默认的 templateResolver 优先
        resolver.setCheckExistence(true);            // 文件不存在就跳过，交给下一个 resolver
        return resolver;
    }
    
}
