package com.izouma.nineth.config;

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.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Value("${storage.local_path}")
    private String localPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        // registry.addResourceHandler("webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/admin/**").addResourceLocations("classpath:/static/admin/");
        registry.addResourceHandler("/files/**").addResourceLocations("file:" + localPath);
        registry.addResourceHandler("/MP_verify*").addResourceLocations("classpath:/");
    }

    @Bean
    public Docket createApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                                 .title("接口文档")
                                 .version("1.0.0")
                                 .termsOfServiceUrl("#")
                                 .description("接口文档")
                                 .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.izouma.nineth.web"))
                .paths(PathSelectors.any())
                .build();
    }

    // @Bean
    // public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
    //     MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    //     //设置日期格式
    //     ObjectMapper objectMapper = new ObjectMapper();
    //     objectMapper.setDateFormat(CustomDateFormat.instance);
    //     objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    //     mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
    //     //设置中文编码格式
    //     List<MediaType> list = new ArrayList<>();
    //     list.add(MediaType.APPLICATION_JSON_UTF8);
    //     mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
    //     return mappingJackson2HttpMessageConverter;
    // }

//    @Override
//    public void addFormatters(FormatterRegistry registry) {
//        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
//        registrar.setUseIsoFormat(true);
//        registrar.registerFormatters(registry);
//    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowCredentials(true)
                .allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH")
                .exposedHeaders("Content-Disposition");
    }

}
