package com.bankaudit.spring.config;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.AuthorizationCodeGrantBuilder;
import springfox.documentation.builders.OAuthBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.CredentialsGrant;
import springfox.documentation.service.GrantType;
import springfox.documentation.service.ResourceOwnerPasswordCredentialsGrant;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.service.TokenEndpoint;
import springfox.documentation.service.TokenRequestEndpoint;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {                                    
    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
          .select()                                  
          .apis(RequestHandlerSelectors.basePackage("com"))              
          .paths(PathSelectors.any())                          
          .build().apiInfo(apiInfo())
          .securitySchemes(Arrays.asList(securityScheme()))
          .securityContexts(Arrays.asList(securityContext()));                                           
    }
    

	@SuppressWarnings("deprecation")
	private ApiInfo apiInfo() {
		ApiInfo apiInfo = new ApiInfo("RCSA NABARD REST API", " Create, Update, Read, Delete   services", "1.0", "BankAudit", "baas@caprusit.com",
				"caprus-licence", "BASS");
		return apiInfo;
	}
	
	@Bean
	public SecurityConfiguration security() {
		Map<String, Object> additionalQueryStringParams=new HashMap<>();
		additionalQueryStringParams.put("legalEntityCode", 3);
		additionalQueryStringParams.put("role", "admin");
		
	    return SecurityConfigurationBuilder.builder()
	        .clientId("bankudit-client-id")
	        .clientSecret("admin@123")
	        .scopeSeparator(" ")
	        .additionalQueryStringParams(additionalQueryStringParams).useBasicAuthenticationWithAccessCodeGrant(false)
	        .build();
	}
	
	private SecurityScheme securityScheme() {
	    GrantType grantType = new ResourceOwnerPasswordCredentialsGrant("http://10.30.2.38:8080/bankaudit/oauth/token?"
	    		+ "legalEntityCode=3"
	    		+ "&userType=admin"
	    		+ "");
	        /*.tokenEndpoint(new TokenEndpoint("http://10.30.2.38:8080/bankaudit/oauth" + "/token", "oauthtoken"))
	        .tokenRequestEndpoint(
	          new TokenRequestEndpoint("http://10.30.2.38:8080/bankaudit/oauth" + "/authorize", "bankudit-client-id", "admin@123"))
	        .build();
	 */
	    SecurityScheme oauth = new OAuthBuilder().name("spring_oauth")
	        .grantTypes(Arrays.asList(grantType))
	        .scopes(Arrays.asList(scopes()))
	        .build();
	    return oauth;
	}
	
	private AuthorizationScope[] scopes() {
	        AuthorizationScope[] scopes = { 
	          new AuthorizationScope("read", "for read operations"), 
	          new AuthorizationScope("write", "for write operations")};
	        return scopes;
	    }
	
	private SecurityContext securityContext() {
	    return SecurityContext.builder()
	      .securityReferences(Arrays.asList(new SecurityReference("spring_oauth", scopes())))
	      .forPaths(PathSelectors.regex("/.*"))
	      .build();
	}
    
}