package com.mosty.common.base.autoconfig.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static com.mosty.common.base.constant.SystemConfigConstants.TOKEN_HEADER;

/**
 *  swagger 配置、自动装配类类
 * {@link ComponentScan} 具体扫描地址可以更改为：
 *  {@link ComponentScan({"com.mosty.common.config.controller","..."})}
 *
 * @author kevin
 * @date 2022/1/22 5:47 PM
 * @since 1.0.0
 */
@Configuration
@EnableSwagger2
@ComponentScan({"com.mosty.common"})
@SuppressWarnings("unused")
public class SwaggerConfig {

    /** swagger基础扫描包 */
    private static final String SWAGGER_SCAN_BASE_PACKAGE = "com.mosty";

    /** swagger默认关闭，则没有配置也保证不会启动报错 */
    @Value("${swagger.enable:true}")
    private Boolean swaggerEnable;
    @Value("${swagger.title:mosty}")
    private String title;
    @Value("${swagger.host:127.0.0.1}")
    private String host;
    @Value("${swagger.port:9999}")
    private String port;
    @Value("${swagger.version:1.0.0}")
    private String version;
    @Value("${swagger.name:基础服务}")
    private String name;
    @Value("${swagger.url:''}")
    private String url;
    @Value("${swagger.email:''}")
    private String email;
    @Value("${spring.application.name}")
    private String serviceName;

    @Bean
    public Docket createRestApi() {
        if (swaggerEnable) {
            List<Parameter> parameters = new ArrayList<>();
            parameters.add(new ParameterBuilder()
                    .name(TOKEN_HEADER)
                    .description("令牌")
                    .modelRef(new ModelRef("string"))
                    .parameterType("header")
                    .required(false)
                    .build());

            return new Docket(DocumentationType.SWAGGER_2)
                    .host(host + ":" + port)
                    .apiInfo(
                            new ApiInfoBuilder().title(serviceName).description(serviceName)
                            .version(version).contact(new Contact(name, url, email)).build())
                    .globalOperationParameters(parameters)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
                    .paths(PathSelectors.any())
                    .build();
        }
        return new Docket(DocumentationType.SWAGGER_2).enable(false);
    }

}
