package com.style24.admin.support.config;

import java.nio.charset.Charset;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.support.MultipartFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.style24.admin.support.interceptor.TsaDefaultInterceptor;
import com.style24.admin.support.interceptor.TsaPosInterceptor;

import com.gagaframework.web.core.filter.GagaXssServletFilter;
import com.gagaframework.web.rest.client.GagaRequestStringTrim;

/**
 * Web MVC Configuration
 *
 * @author gagamel
 * @since 2020. 10. 5
 */
@Configuration
public class TsaWebMvcConfig implements WebMvcConfigurer {

	@Autowired
	private TsaDefaultInterceptor defaultInterceptor;

	@Autowired
	private TsaPosInterceptor posInterceptor;

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		final String[] excludePathPatterns = new String[] {
			"/", "/index", "/signin", "/image/**", "/ux/**", "/smartEditor/**",
			"/error/**", "/data/**", "/login", "/logout",
			"/pos/**", "/goods/naverEP/download", "/shoplinker/**"
		};

		registry.addInterceptor(defaultInterceptor)
			.addPathPatterns("/**/*")
			.excludePathPatterns(excludePathPatterns);

		registry.addInterceptor(posInterceptor)
			.addPathPatterns("/pos/**")
			.excludePathPatterns("/pos/login/**");
	}

	/**
	 * @RequestBody annotation 이용 시 json data 형식의 모든 문자열의 앞, 뒤 공백을 제거
	 */
	@Bean
	public GagaRequestStringTrim stringTrim() {
		return new GagaRequestStringTrim();
	}

	/**
	 * XSS(Cross Site Script) Prevention Filter
	 *
	 * @return
	 */
	@SuppressWarnings({"rawtypes", "unchecked"})
	@Bean
	public FilterRegistrationBean xssFilterRegistrationBean() {
		FilterRegistrationBean bean = new FilterRegistrationBean();
		bean.setFilter(new GagaXssServletFilter());
		bean.setOrder(2);
		bean.addUrlPatterns("/*");
		return bean;
	}

	/**
	 * Multipart Filter
	 * 파일 업로드 구현 시 MultipartRequest를 처리함에 따라 해당 부분은 불필요 하나,
	 * com.oreilly.servlet.MultipartRequest 같은 걸 이용 시
	 * "Corrupt form data: premature ending" exception이 발생한다.
	 * 이를 해결하고자 Filter를 구성
	 *
	 * @return
	 */
	@SuppressWarnings({"rawtypes", "unchecked"})
	@Bean
	public FilterRegistrationBean springMultipartRegistrationBean() {
		FilterRegistrationBean bean = new FilterRegistrationBean();
		bean.setName("springMultipartResolver");
		bean.setFilter(new MultipartFilter());
		bean.setOrder(1);
		bean.addUrlPatterns(new String[] {
			"/common/file/upload/**",
			"/common/files/upload/**"
		});
		return bean;
	}

	/**
	 * API 호출을 위한 RestTemplate 설정
	 *
	 * @return
	 */
	@Bean
	public RestTemplate restTemplate() {
		HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
		factory.setConnectTimeout(300000);
		factory.setReadTimeout(300000);

		RestTemplate restTemplate = new RestTemplate(factory);

		// Convert the message to UTF-8
		restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

		return restTemplate;
	}

}
