package com.dhcc.finance.config;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;

import com.dhcc.common.constant.SysConstants;
import com.google.common.collect.Lists;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
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;

/**
 * 
 * @功能描述：
 * @author chenrui
 * @date 2019年9月17日 下午5:49:31
 * @修改日志：
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
	
	@Value("${spring.application.name}")
	private String serviceName;
 
	@Autowired
	ClientCredentialsResourceDetails clientCredentialsResourceDetails;
	
	@Bean
    public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }
	
	private ApiInfo apiInfo() {
		Contact contact = new Contact("东华软件","www.dhcc.com.cn","62662288");
		return new ApiInfoBuilder()
            .title(serviceName + "服务接口字典")
            .description("REST API接口统一响应接口实体")
            .contact(contact)
            .version("1.0")
            .build();
	}
	
	 private List<ApiKey> securitySchemes() {
	        List<ApiKey> apiKeyList = Lists.newArrayList();
	        apiKeyList.add(new ApiKey(HttpHeaders.AUTHORIZATION, HttpHeaders.AUTHORIZATION, "header"));
	        apiKeyList.add(new ApiKey(SysConstants.HEADER_PARAMETER.CACHE_IDENTITY, SysConstants.HEADER_PARAMETER.CACHE_IDENTITY, "header"));
	        return apiKeyList;
	    }
	 
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = Lists.newArrayList();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }
 
    List<SecurityReference> defaultAuth() {
    	final AuthorizationScope[] authorizationScopes = new AuthorizationScope[3];
        authorizationScopes[0] = new AuthorizationScope("read", "read all");
        authorizationScopes[1] = new AuthorizationScope("trust", "trust all");
        authorizationScopes[2] = new AuthorizationScope("write", "write all");
        List<SecurityReference> securityReferences = Lists.newArrayList();
        securityReferences.add(new SecurityReference(HttpHeaders.AUTHORIZATION, authorizationScopes));
        securityReferences.add(new SecurityReference(SysConstants.HEADER_PARAMETER.CACHE_IDENTITY, authorizationScopes));
        return securityReferences;
    }
	 

}
