package com.jay.monitor.data.server.config;

import com.jay.monitor.data.server.costs.ServerConstants;
import com.jay.monitor.data.server.interceptors.LoginHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * mvc层定义
 *
 * @author Liuhx
 * @create 2018/6/14 18:41
 * @email liuhx@elab-plus.com
 **/
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.jay.monitor.data.server.controllers"})
public class MvcConfigBean extends WebMvcConfigurerAdapter {

    @Autowired
    private LoginHandler loginHandler;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
        registry.addResourceHandler("/lib/**").addResourceLocations("classpath:/static/lib/");
        registry.addResourceHandler("/fonts/**").addResourceLocations("classpath:/static/fonts/");
        registry.addResourceHandler("/images/**").addResourceLocations("classpath:/static/images/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginHandler).addPathPatterns("/**")
            .excludePathPatterns(ServerConstants.LOGIN_HTML_PATH, ServerConstants.LOGIN_PATH)
            .excludePathPatterns("/js/**", "/css/**", "/lib/**", "/fonts/**", "/images/**");
    }

    //    @Bean
    //    public TemplateEngine layoutDialect() {
    //        // Or SpringTemplateEngine for Spring
    //        TemplateEngine templateEngine = new TemplateEngine();
    //        templateEngine.addDialect(new LayoutDialect(new GroupingStrategy()));
    //        return templateEngine;
    //    }
}
