package com.dayou.configuration;

import com.dayou.ueditor.ActionEnter;
import com.dayou.ueditor.ConfigManager;
import com.dayou.ueditor.UEditorConfig;
import com.google.common.cache.Cache;
import com.dayou.filter.AFilter1_CorsFilter;
import com.dayou.interceptor.LoginInterceptor;
import com.dayou.interceptor.PermissionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
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 kids
 */
@Configuration(proxyBeanMethods = false)
public class WebConfig implements WebMvcConfigurer {


    @Autowired
    private LoginInterceptor loginInterceptor;

    @Autowired
    private PermissionInterceptor permissionInterceptor;

    @Autowired
    private UEditorConfig uEditorConfig;




    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /** smart-doc 文档配置*/
        registry.addResourceHandler("/doc/**").addResourceLocations("classpath:/static/doc/");

    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS")
                .allowedHeaders("X-Requested-With", "Content-Type", "token")
                .maxAge(3600);
    }


    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/*/dfs/**")
                .excludePathPatterns("/dfs/**")
                .excludePathPatterns("/user/login", "/gisUser/login", "/gisUser/verification",
                        "/gisVisited", "/globalConfig/getSysCfg", "/sdk/**", "/error","/onlyoffice/**",
                        "/poserver.zz","/poclient","/sealsetup.exe","/doc/saveFile","/auth/checkOaToken");

        registry.addInterceptor(permissionInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/dfs/**")
                .excludePathPatterns("/doc/**", "/sdk/**","/callback/**");

    }

    @Bean
    public FilterRegistrationBean testFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new AFilter1_CorsFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("CorsFilter");
        registration.setOrder(100);
        return registration;
    }

    @Bean
    @ConditionalOnMissingBean(ActionEnter.class)
    public ActionEnter actionEnter() {
        ActionEnter actionEnter = new ActionEnter(ConfigManager.getInstance(uEditorConfig));
        return actionEnter;
    }


//    @Bean
//    public WebMvcConfigurer corsConfigurer() {
//        return new WebMvcConfigurer() {
//            @Override
//            public void addCorsMappings(CorsRegistry registry) {
//                registry.addMapping("/**")
//                        .allowedOrigins("*")
//                        .allowedMethods("*")
//                        .maxAge(3600);
//            }
//
//            @Override
//            public void addFormatters(FormatterRegistry registry) {
//                registry.addFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
//            }
//
//            @Override
//            public void addInterceptors(InterceptorRegistry registry) {
//                registry.addInterceptor(commonInterceptor)
//                        .addPathPatterns("/**");
//                registry.addInterceptor(loginInterceptor)
//                        .addPathPatterns("/**")
//                        .excludePathPatterns("/user/login","/doc/**");
//            }
//        };
//    }

}
