package org.javamaster.spring.swagger;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.javamaster.spring.swagger.convert.SexEnumConvert;
import org.javamaster.spring.swagger.convert.SexEnumFormatter;
import org.javamaster.spring.swagger.convert.SexEnumGenericConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Date类型序列化为long返回
 *
 * @author yudong
 * @date 2022/1/4
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (HttpMessageConverter<?> converter : converters) {
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                MappingJackson2HttpMessageConverter messageConverter = (MappingJackson2HttpMessageConverter) converter;
                ObjectMapper objectMapper = messageConverter.getObjectMapper();
                SimpleModule simpleModule = new SimpleModule();
                simpleModule.addSerializer(Date.class, new JsonSerializer<Date>() {
                    @Override
                    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
                        if (date == null) {
                            jsonGenerator.writeNull();
                        } else {
                            jsonGenerator.writeNumber(date.getTime());
                        }
                    }
                });
                objectMapper.registerModule(simpleModule);
            }
        }
    }

    @Override
    public void addFormatters(FormatterRegistry formatterRegistry) {
        formatterRegistry.addFormatter(new SexEnumFormatter());

        // formatterRegistry.addConverter(new SexEnumConvert());

        // formatterRegistry.addConverter(new SexEnumGenericConverter());
    }
}
