package com.icomsys.main_vm.common.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.apache.http.client.HttpClient;



@Configuration
@Slf4j
public class ConfigMvc extends WebMvcConfigurationSupport {

    @Value("${file.path}")
    private String filePath;
//    @Value("${Server.servlet.context-path}")
//    private String context;



    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /*
        Path path = Paths.get(filePath).toAbsolutePath().normalize();
        String patsString = path.toString();
        log.info("path = {}",path);
        log.info("pathString = {}",patsString);
        */
        registry.addResourceHandler("/**","/img/**")
                .addResourceLocations("/");
//                .addResourceLocations("file:///"+patsString+"/");
        registry.addResourceHandler("/files/**").addResourceLocations("file:/logs/was/aicb/cinnamon_ui/files/");

    }


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
//		registry.addViewController("/**/*.do").setViewName("redirect:/adm/main/egovLoginUsr.do");
//        registry.addViewController("/").setViewName("redirect:/adm/main/egovLoginUsr.do");
//        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/**").addResourceLocations("/");
//    }

    @Bean
    public UrlBasedViewResolver urlBasedViewResolver() {
        UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
        urlBasedViewResolver.setOrder(1);
        urlBasedViewResolver.setViewClass(JstlView.class);
        urlBasedViewResolver.setPrefix("/WEB-INF/jsp/");
        urlBasedViewResolver.setSuffix(".jsp");
        return urlBasedViewResolver;
    }

    @Bean
    public SessionLocaleResolver localeResolver() {
        return new SessionLocaleResolver();
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
        interceptor.setParamName("language");
        return interceptor;
    }

    @Bean
    public MappingJackson2JsonView jsonView() {
        MappingJackson2JsonView jackson2JsonView = new MappingJackson2JsonView();
//        jackson2JsonView.setContentType("\"text/html;charset=UTF-8\"");
        return jackson2JsonView;
    }

//    @Override
//    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
//        Properties prop = new Properties();
//        prop.setProperty("org.springframework.dao.DataAccessException", "cmm/error/dataAccessFailure");
//        prop.setProperty("org.springframework.transaction.TransactionException", "cmm/error/transactionFailure");
//        prop.setProperty("org.springframework.security.AccessDeniedException", "cmm/error/accessDenied");
//        prop.setProperty("java.lang.Throwable", "cmm/error/egovBizException");
//        prop.setProperty("java.lang.NoSuchFieldException", "cmm/error/egovError");
//        prop.setProperty("javax.el.PropertyNotFoundException", "cmm/error/egovError");
//        prop.setProperty("org.springframework.web.servlet.PageNotFound", "cmm/error/egovError");
//
//        Properties statusCode = new Properties();
//        statusCode.setProperty("cmm/error/egovBizException", "400");
//        statusCode.setProperty("cmm/error/accessDenied", "401");
//        statusCode.setProperty("cmm/error/accessDenied", "403");
//        statusCode.setProperty("cmm/error/egovError", "404");
//        statusCode.setProperty("cmm/error/egovBizException", "500");
//        SimpleMappingExceptionResolver smer = new SimpleMappingExceptionResolver();
//        smer.setDefaultErrorView("cmm/error/egovError");
//        smer.setExceptionMappings(prop);
//        smer.setStatusCodes(statusCode);
//        exceptionResolvers.add(smer);
//    }



    @Bean
    public RestTemplate restTemplate() {
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        HttpClient client = HttpClientBuilder.create()
                .setMaxConnTotal(50)
                .setMaxConnPerRoute(20).build();
        factory.setHttpClient(client);
        factory.setConnectTimeout(10000);
        factory.setReadTimeout(5000);
        return new RestTemplate(factory);
    }

}
