package com.konkerlabs.platform.registry.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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.swagger.web.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.google.common.collect.Lists.newArrayList;

@EnableWebMvc
@Configuration
@EnableSwagger2
public class SwaggerUIConfig extends WebMvcConfigurerAdapter {

    public static final String securitySchemaOAuth2 = "oauth2schema";
    public static final String authorizationScopeTrust = "trust";
    public static final String authorizationScopeRead = "read";
    public static final String authorizationScopeWrite = "write";
    public static final String authorizationScopeGlobalDesc ="Access Iot Resources";


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo())
                .securitySchemes(newArrayList(securitySchema()))
                .securityContexts(newArrayList(securityContext()))
                .enableUrlTemplating(true);

    }


    private BasicAuth basicSecuritySchema() {
        return new BasicAuth("basic");
    }

    private OAuth securitySchema() {

        ClientCredentialsGrant cliGrantType =
                new ClientCredentialsGrant("/v1/oauth/token");

        return new OAuth(
                securitySchemaOAuth2,
                newArrayList(
                        new AuthorizationScope(authorizationScopeTrust, authorizationScopeGlobalDesc),
                        new AuthorizationScope(authorizationScopeRead, authorizationScopeGlobalDesc),
                        new AuthorizationScope(authorizationScopeWrite, authorizationScopeGlobalDesc)
                ),
                newArrayList(cliGrantType));
    }

    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.any())
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[3];
        authorizationScopes[0] = new AuthorizationScope(authorizationScopeTrust, authorizationScopeGlobalDesc);
        authorizationScopes[1] = new AuthorizationScope(authorizationScopeRead, authorizationScopeGlobalDesc);
        authorizationScopes[2] = new AuthorizationScope(authorizationScopeWrite, authorizationScopeGlobalDesc);
        return newArrayList(
                new SecurityReference(securitySchemaOAuth2, authorizationScopes));
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("Konker Platform Api")
                .description(
                        "Before access endpoints please login with your " +
                                "<a href='/v1/oauth/token?grant_type=client_credentials'>account</a>")
                .termsOfServiceUrl("https://demo.konkerlabs.net/registry/resources/konker/pdf/termos_de_uso_20161014a-9d089e3f67c4b4ab9c83c0a0313158ef.pdf")
                .contact(new Contact("Konker", "developers.konkerlabs.com", "support@konkerlabs.com"))
                .license("Apache 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
                .version("v2")
                .build();
    }


}

@@ -4183,24 +4183,8 @@
 ase 
-login with your 
 %22 +%0A
@@ -4275,16 +4275,30 @@
 als'
-%3Eaccount
+ target='_blank'%3Elogin
 %3C/a%3E

improve login text