package com.oqpo.api.config;

import com.navercorp.lucy.security.xss.servletfilter.XssEscapeServletFilter;
import com.oqpo.api.web.filter.HttpRequestWrapperFilter;
import com.oqpo.api.web.interceptor.TokenCheckInterceptor;
import com.oqpo.api.web.resolver.ArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;
import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private ArgumentResolver argumentResolver;

    @Autowired
    private TokenCheckInterceptor tokenCheckInterceptor;

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(tokenCheckInterceptor).addPathPatterns("/api/**")
                        .excludePathPatterns("/api/member/sign-in")
                        .excludePathPatterns("/api/member/sign-in-app")
                        .excludePathPatterns("/api/member/sign-in-swagger")
                        .excludePathPatterns("/api/session/hashed")
                        .excludePathPatterns("/api/session/expire")
                        .excludePathPatterns("/api/pop/open-pop")
                        .excludePathPatterns("/api/pop/info-pop")
                        .excludePathPatterns("/api/member/find-id")
                        .excludePathPatterns("/api/member/find-pw")
                        .excludePathPatterns("/api/verinfo/info-lastver")
                        .excludePathPatterns("/api/session/bsnsbrand")
                        .excludePathPatterns("/api/file/download/**");
            }

            @Override
            public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
                argumentResolvers.add(argumentResolver);
            }

            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        //.allowedOrigins("http://localhost:8091", "http://106.246.249.162:38091")
                        .allowedOrigins("*")
                        .allowedMethods("GET", "POST", "PUT", "PATCH", "OPTIONS", "HEAD", "DELETE")
                        .allowedHeaders("Origin", "Product-Session", "X-Requested-With", "X-HTTP-Method-Override", "Content-Type", "Accept", "Referer", "User-Agent", "x-auth-token")
                        .allowCredentials(true)
                        .maxAge(3600);
            }
        };
    }

    @Bean
    public FilterRegistrationBean<XssEscapeServletFilter> filterRegistrationBean() {
        FilterRegistrationBean<XssEscapeServletFilter> filterRegistration = new FilterRegistrationBean<>();
        filterRegistration.setFilter(new XssEscapeServletFilter());
        filterRegistration.setOrder(1);
        filterRegistration.addUrlPatterns("/*");
        return filterRegistration;
    }

    @Bean
    public FilterRegistrationBean<HttpRequestWrapperFilter> filterBean() {
        final FilterRegistrationBean<HttpRequestWrapperFilter> filterRegBean = new FilterRegistrationBean<HttpRequestWrapperFilter>();
        filterRegBean.setFilter(new HttpRequestWrapperFilter());
        filterRegBean.addUrlPatterns("/*");
        return filterRegBean;
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize(1024 * 1024 * 100); //"100MB"
        //factory.setMaxRequestSize(getMaxUploadFileSize());
        return factory.createMultipartConfig();
    }

    @Bean
    public MultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }
}