package com.kexue.skills.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**  mvc配置，例如：资源映射、视图解析、拦截器等
 *
 **/

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

//    @Autowired
//    SessionTimeoutInterceptor sessionTimeoutInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/assets/ckeditor/**").
                addResourceLocations("classpath:/static/assets/ckeditor/").
                setCachePeriod(2592000);

        registry.addResourceHandler("/upload/**").addResourceLocations("file:E://01code//bizcard//upload");
        
        // 配置Swagger UI资源映射
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/admin").setViewName("admin");
        
        // 将 /doc.html 重定向到 /swagger-ui/index.html
        registry.addRedirectViewController("/doc.html", "/swagger-ui/index.html");
        
        /*
        registry.addViewController("/index").setViewName("index");

        registry.addViewController("/addNew").setViewName("addNew");
        registry.addViewController("/myArticle").setViewName("myArticle");
        registry.addViewController("/myInformation").setViewName("myInformation");
        registry.addViewController("/register").setViewName("register");
        registry.addViewController("/submitJournalContegory").setViewName("submitJournalContegory");
        registry.addViewController("/submitJournalJournalPrevious").setViewName("submitJournalContegory");
        registry.addViewController("/submitJournalJournal").setViewName("submitJournalJournal");
        registry.addViewController("/submitJournalUpload").setViewName("submitJournalUpload");*/
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 配置字符串转换器，使用UTF-8编码
        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        converters.add(stringConverter);
        
        // 配置JSON转换器，使用UTF-8编码
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        jsonConverter.setDefaultCharset(StandardCharsets.UTF_8);
        converters.add(jsonConverter);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//       registry.addInterceptor(sessionTimeoutInterceptor).addPathPatterns("/**").
//               excludePathPatterns("/toRegister","/getVerifyCode", "/api/**","/index","/doLogin","/login","/register", "/addNew", "/static/**","/logout","/upload/");
    }
}
