package com.xinelu.framework.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

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

/**
 * swagger2接口文档配置
 */
@Configuration
@EnableKnife4j
@EnableSwagger2
@Import(BeanValidatorPluginsConfiguration.class)
public class Knife4jConfig {
    @Value("${token.header}")
    private String tokenHeader;
    @Value("${spring.profiles.active}")
    private String profile;
    @Value("${swagger-ui.base-url}")
    private String swaggerUrl;

    @Bean
    public Docket docket(Environment environment) {
        Profiles profiles = Profiles.of(profile);
        RequestParameterBuilder tokenPar = new RequestParameterBuilder();
        List<RequestParameter> pars = new ArrayList<>();
        tokenPar.name(tokenHeader).in(ParameterType.HEADER).description("令牌").required(false).build();
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(environment.acceptsProfiles(profiles))
                .apiInfo(apiInfo())
                .securityContexts(Collections.singletonList(securityContext()))
                .securitySchemes(Collections.singletonList(apiKey()))
                .select()
                .paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.basePackage(swaggerUrl))
                .build()
                .globalRequestParameters(pars);
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().version("v3.0.0")
                .title("新医路平台接口文档")
                .build();
    }

    private ApiKey apiKey() {
        return new ApiKey("token令牌", "Authorization", "header");
    }

    private SecurityContext securityContext() {
        return SecurityContext.builder().securityReferences(securityReferences()).build();
    }

    private List<SecurityReference> securityReferences() {
        AuthorizationScope[] scopes = {new AuthorizationScope("global", "accessEverything")};
        return Collections.singletonList(new SecurityReference("token令牌", scopes));
    }
}
