package com.sajo.teamkerbell.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Created by seongahjo on 2016. 7. 14..
 */


@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.sajo.teamkerbell.controller"})

public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        registry.addResourceHandler("/favicon.ico").addResourceLocations("/img/fairy.ico");
    }

    @Bean
    MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(100000000);
        multipartResolver.setMaxInMemorySize(100000000);
        return multipartResolver;
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("message/messages");
        return messageSource;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new Hibernate5Module());
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        jackson2HttpMessageConverter.setPrettyPrint(true);

        converters.add(stringHttpMessageConverter);
        converters.add(jackson2HttpMessageConverter);
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.useRegisteredExtensionsOnly(true)
                .defaultContentType(MediaType.APPLICATION_JSON)
                .mediaType("json", MediaType.APPLICATION_JSON);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new SessionInterceptor())
//                .addPathPatterns("/*")
//                .excludePathPatterns("/resources/*")
//                .excludePathPatterns("/login");
    }


    /*
    @Bean
    VertxServer vertxServer() {
        return new VertxServer();
    }
    */


}

@@ -127,34 +127,155 @@
 data
-type.hibernate5.Hibernate5
+bind.SerializationFeature;%0Aimport com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;%0Aimport com.fasterxml.jackson.datatype.jsr310.JavaTime
 Modu
@@ -2936,16 +2936,162 @@
 ule());%0A
+        objectMapper.registerModule(new JavaTimeModule());%0A        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);%0A
         
@@ -3734,398 +3734,7 @@
    %7D
-%0A%0A    @Override%0A    public void addInterceptors(InterceptorRegistry registry) %7B%0A//        registry.addInterceptor(new SessionInterceptor())%0A//                .addPathPatterns(%22/*%22)%0A//                .excludePathPatterns(%22/resources/*%22)%0A//                .excludePathPatterns(%22/login%22);%0A    %7D%0A%0A%0A    /*%0A    @Bean%0A    VertxServer vertxServer() %7B%0A        return new VertxServer();%0A    %7D%0A    */%0A%0A
 %0A%7D%0A

Add jackson module for java8 time