package com.tofly.common.oauth.config;

import com.tofly.common.core.configs.DocBean;
import com.tofly.common.core.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
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.ApiInfo;
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;

/**
 * @author Administrator
 * @date 2019/9/25  16:28
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Autowired
    private DocBean docBean;
     
    /*@Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2).
                select().
                apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build();
    }*/

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//                .apis(RequestHandlerSelectors.basePackage("com.tofly"))
                .apis(
                    //StringUtil.isEmpty(docBean.getScan())? RequestHandlerSelectors.withClassAnnotation(ApiOperation.class)
                        StringUtil.isEmpty(docBean.getScan())? RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)
                            :RequestHandlerSelectors.basePackage(docBean.getScan())
                )
                .paths(PathSelectors.any())
                .build()
                //增加token参数方便调试
                //.globalOperationParameters(globalOperation())
                ;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(StringUtil.isEmpty(docBean.getTitle())?"JAVA架构接口文档":docBean.getTitle())
                .description(StringUtil.isEmpty(docBean.getDescription())?"接口描述":docBean.getDescription())
                .termsOfServiceUrl("")
                .contact(new Contact(StringUtil.isEmpty(docBean.getAuthor())?"admin":docBean.getAuthor(), "", ""))
                .version(StringUtil.isEmpty(docBean.getVersion())?"1.0":docBean.getVersion())
                .build();
    }

    private List<Parameter> globalOperation(){
        //添加head参数配置start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        //第一个token为传参的key，第二个token为swagger页面显示的值
        tokenPar.name("Authorization").description("需要进行验证token，以‘bearer xxxx’格式录入").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        return pars;
    }
     
}