/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .F88X
 *        X8888Y
 *      .}888888N;
 *        i888888N;        .:!              .I$WI:
 *          R888888I      .'N88~            i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
 *          .R888888I    .;N8888~          .X8'  "8I.!,/8"  !%NY8`"8I8~~8>,88I
 *            +888888N;  .8888888Y                                  "&&8Y.}8,
 *            ./888888N;  .R888888Y        .'}~    .>}'.`+>  i}!    "i'  +/'  .'i~  !11,.:">,  .~]!  .i}i
 *              ~888888%:  .I888888l      .]88~`1/iY88Ii+1'.R$8$8]"888888888>  Y8$  W8E  X8E  W8888'188Il}Y88$*
 *              18888888    E8888881    .]W%8$`R8X'&8%++N8i,8N%N8+l8%`  .}8N:.R$RE%N88N%N$K$R  188,FE$8%~Y88I
 *            .E888888I  .i8888888'      .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
 *            8888888I  .,N888888~        ~88i"8W,!N8*.I88.}888%F,i$88"F88"  888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
 *          i888888N'      I888Y          ]88;/EX*IFKFK88X  K8R  .l8W  88Y  ~88}'88E&%8W.X8N``]88!.$8K  .:W8I
 *        .i888888N;        I8Y          .&8$  .X88!  i881.:%888>I88  ;88]  +88+.';;;;:.Y88X  18N.,88l  .+88/
 *      .:R888888I
 *      .&888888I                                          Copyright (c) 2009-2020.  博锐尚格科技股份有限公司
 *        ~8888'
 *        .!88~                                                                     All rights reserved.
 *
 * *********************************************************************************************************************
 */

package com.persagy.server.syn

import io.swagger.v3.oas.annotations.enums.ParameterIn
import io.swagger.v3.oas.models.Components
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.Operation
import io.swagger.v3.oas.models.info.Contact
import io.swagger.v3.oas.models.info.Info
import io.swagger.v3.oas.models.info.License
import io.swagger.v3.oas.models.media.StringSchema
import io.swagger.v3.oas.models.parameters.Parameter
import io.swagger.v3.oas.models.security.SecurityScheme
import org.springdoc.core.GroupedOpenApi
import org.springdoc.core.customizers.OperationCustomizer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.method.HandlerMethod

/**
 * RESTful API文档生成器Swagger2配置
 *
 * @author  PLX
 */
@Configuration
open class Swagger3Config {

    /** 标题 */
    private val title = "数据同步 API"
    /** API 版本号 */
    private val version = "2.0"

    val operationCustomizer = GlobalHeaderOperationCustomizer()

    /**
     * API 信息
     */
    @Bean
    open fun openApi(): OpenAPI {
        val contact = Contact().name("张维新").email("zhangweiixn@sagacloud.cn")
        return OpenAPI()
            .components(
                Components()
                .addSecuritySchemes("basicScheme", SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic")))
            .info(
                Info()
                .title(title)
                .version(version)
                .termsOfService("http://www.persagy.com")
                .contact(contact)
                .license(
                    License()
                    .name("Apache 2.0")
                    .url("http://www.persagy.com")))
    }



    /**
     * 默认分组
     */
    @Bean
    open fun defaultApiGroup(): GroupedOpenApi {
        return GroupedOpenApi.builder()
            .group("默认")
            .pathsToMatch("/**")
            .build()
    }

    /**
     * 默认分组
     */
    @Bean
    open fun dictApiGroup(): GroupedOpenApi {
        return GroupedOpenApi.builder()
            .group("字典")
            .pathsToMatch("/dict/**")
            .build()
    }

    /**
     * 默认分组
     */
    @Bean
    open fun dataCenterApiGroup(): GroupedOpenApi {
        return GroupedOpenApi.builder()
            .group("同步-数据中心->中台")
            .pathsToMatch("/datacenter/**")
            .build()
    }


    /**
     * 全局参数
     *
     * @author  庞利祥 <sybotan@126.com>
     */
    class GlobalHeaderOperationCustomizer : OperationCustomizer {
        /**
         * 接口定义器
         *
         * @param   operation       接口
         * @param   handlerMethod   原接口方法
         * @return  新定义的接口
         */
        override fun customize(operation: Operation, handlerMethod: HandlerMethod): Operation {
            val projectId = Parameter().`in`(ParameterIn.HEADER.toString())
                .name("projectId").description("项目 id")
                .schema(StringSchema()).required(false)
            operation.addParametersItem(projectId)
            return operation
        }
    }

} // Class Swagger2Config