package cn.superdesk.uniorder.config.swagger3;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
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.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
//import springfox.documentation.swagger2.annotations.EnableSwagger2;
//import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableKnife4j
@EnableSwagger2
//@EnableSwagger2WebMvc
public class Knife4jConfig {


    /*引入Knife4j提供的扩展类*/
    private final OpenApiExtensionResolver openApiExtensionResolver;

    @Autowired
    public Knife4jConfig(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    @Bean
    public Docket createRestApiForAll() {

        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller"))
                .paths(PathSelectors.any())
                .build()
                .groupName("default")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("default"))
                ;
    }

    @Bean
    public Docket createRestApiForCommon() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller._common"))
                .paths(PathSelectors.any())
                .build()
                .groupName("_common")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("_common"))
                ;
    }

    /**
     * organisation
     * Organisation
     *
     * @return
     */
    @Bean
    public Docket createRestApiForOrganisation() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller.organisation"))
                .paths(PathSelectors.any())
                .build()
                .groupName("organisation")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("organisation"))
                ;
    }

    // cn.superdesk.uniorder.controller.scheduler
    @Bean
    public Docket createRestApiForScheduler() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller.scheduler"))
                .paths(PathSelectors.any())
                .build()
                .groupName("scheduler")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("scheduler"))
                ;
    }

    /**
     * statistics
     * statistics
     *
     * @return
     */
    @Bean
    public Docket createRestApiForStatistics() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller.statistics"))
                .paths(PathSelectors.any())
                .build()
                .groupName("statistics")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("statistics"))
                ;
    }


    /**
     * sys
     * Sys
     *
     * @return
     */
    @Bean
    public Docket createRestApiForSys() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller.sys"))
                .paths(PathSelectors.any())
                .build()
                .groupName("sys")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("sys"))
                ;
    }


    @Bean
    public Docket createRestApiForAccountingCustomer() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller.accounting.customer"))
                .paths(PathSelectors.any())
                .build()
                .groupName("accounting.customer")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("accounting.customer"))
                ;
    }


    @Bean
    public Docket createRestApiForInvoiceCommon() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller.invoice.common"))
                .paths(PathSelectors.any())
                .build()
                .groupName("invoice.common")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("invoice.common"))
                ;
    }

    @Bean
    public Docket createRestApiForInvoiceCustomer() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller.invoice.customer"))
                .paths(PathSelectors.any())
                .build()
                .groupName("invoice.customer")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("invoice.customer"))
                ;
    }

    @Bean
    public Docket createRestApiForInvoiceSupplier() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller.invoice.supplier"))
                .paths(PathSelectors.any())
                .build()
                .groupName("invoice.supplier")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("invoice.supplier"))
                ;
    }

    @Bean
    public Docket createRestApiForAccountingSupplier() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller.accounting.supplier"))
                .paths(PathSelectors.any())
                .build()
                .groupName("accounting.supplier")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("accounting.supplier"))
                ;
    }


    // cn.superdesk.uniorder.controller._thirdpart.szhtxx.electronicInvoice
    @Bean
    public Docket createRestApiForThirdpartSzhtxxElectronicInvoice() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller._thirdpart.szhtxx.electronicInvoice"))
                .paths(PathSelectors.any())
                .build()
                .groupName("_thirdpart.szhtxx.electronicInvoice")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("_thirdpart.szhtxx.electronicInvoice"))
                ;
    }

    // cn.superdesk.uniorder.controller._thirdpart.szhtxx.paperInvoice
    @Bean
    public Docket createRestApiForThirdpartSzhtxxPaperInvoice() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller._thirdpart.szhtxx.paperInvoice"))
                .paths(PathSelectors.any())
                .build()
                .groupName("_thirdpart.szhtxx.paperInvoice")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("_thirdpart.szhtxx.paperInvoice"))
                ;
    }




    // cn.superdesk.uniorder.controller._thirdpart.jd
    @Bean
    public Docket createRestApiForThirdpartJd() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.controller._thirdpart.jd"))
                .paths(PathSelectors.any())
                .build()
                .groupName("_thirdpart.jd")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("_thirdpart.jd"))
                ;
    }


    // cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.electronicInvoice._mock
//    @Bean
//    public Docket createRestApiForOpenapiCallThirdpartSzhtxxElectronicInvoiceMock() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .useDefaultResponseMessages(false)
//                .apiInfo(apiInfo())
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.electronicInvoice._mock"))
//                .paths(PathSelectors.any())
//                .build()
//                .groupName("openapicall._thirdpart.szhtxx.electronicInvoice._mock")
//                .pathMapping("/")
//                // 赋予插件体系
//                .extensions(openApiExtensionResolver.buildExtensions("openapicall._thirdpart.szhtxx.electronicInvoice._mock"))
//                ;
//    }


    @Bean
    public Docket createRestApiForOpenApiOrder() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.openapi.api.order"))
                .paths(PathSelectors.any())
                .build()
                .groupName("openapi.order")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("openapi.order"))
                ;
    }

    @Bean
    public Docket createRestApiForOpenApiAccountingCustomer() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.openapi.api.accounting.customer"))
                .paths(PathSelectors.any())
                .build()
                .groupName("openapi.accounting.customer")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("openapi.accounting.customer"))
                ;
    }


//	cn.superdesk.uniorder.openapi.api._thirdpart.qchjc
    @Bean
    public Docket createRestApiForOpenApiThirdpartQchjc() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.superdesk.uniorder.openapi.api._thirdpart.qchjc"))
                .paths(PathSelectors.any())
                .build()
                .groupName("openapi._thirdpart.qchjc")
                .pathMapping("/")
                // 赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions("openapi._thirdpart.qchjc"))
                ;
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Superdesk UniOrder Services APIs")
                .description("Superdesk UniOrder Services Readme")
                .termsOfServiceUrl("https://www.superdesk.cn/superdesk-uniorder")
                .contact(new Contact("linjinyu", "", "linjinyu@avic-s.com"))
                .version("1.0.0")
                .build();
    }

}
