package org.cbioportal.web.config;

import org.cbioportal.web.config.annotation.InternalApi;
import org.cbioportal.web.config.annotation.PublicApi;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;

@Configuration
@EnableSwagger2
@PropertySource("classpath:springfox.properties")
public class SwaggerConfig {

    @Bean
    public Docket publicApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.withClassAnnotation(PublicApi.class))
            .build()
            .useDefaultResponseMessages(false)
            .protocols(new HashSet<>(Arrays.asList("http", "https")))
            .apiInfo(apiInfo());
    }

    @Bean
    public Docket internalApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("internal")
            .select()
            .apis(RequestHandlerSelectors.withClassAnnotation(InternalApi.class))
            .build()
            .useDefaultResponseMessages(false)
            .protocols(new HashSet<>(Arrays.asList("http", "https")))
            .apiInfo(apiInfo());
    }

    @Bean
    UiConfiguration uiConfig() {
        return UiConfigurationBuilder.builder()
            .displayRequestDuration(true)
            .validatorUrl("")
            .build();
    }

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo(
            "cBioPortal web API [Beta]",
            "A web service for supplying JSON formatted data to cBioPortal clients. " +
                "Please note that this API is currently in beta and subject to change.",
            "1.0 (beta)",
            null,
            new Contact("cBioPortal", "http://www.cbioportal.org", "cbioportal@googlegroups.com"),
            "License",
            "https://github.com/cBioPortal/cbioportal/blob/master/LICENSE", Collections.emptyList());
        return apiInfo;
    }
}

@@ -29,16 +29,63 @@
 onfig;%0A%0A
+import org.cbioportal.model.ClinicalDataCount;%0A
 import o
@@ -129,24 +129,24 @@
 nternalApi;%0A
-
 import org.c
@@ -253,16 +253,126 @@
 ectors;%0A
+import springfox.documentation.schema.AlternateTypeRules;%0Aimport springfox.documentation.schema.WildcardType;%0A
 import s
@@ -570,16 +570,80 @@
 .Docket;
+%0A%0Aimport org.springframework.beans.factory.annotation.Autowired;
 %0Aimport 
@@ -810,16 +810,58 @@
 Source;%0A
+import org.springframework.core.Ordered;%0A%0A
 import s
@@ -1128,16 +1128,107 @@
 HashSet;
+%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Aimport com.fasterxml.classmate.TypeResolver;
 %0A%0A@Confi
@@ -1332,16 +1332,71 @@
 nfig %7B%0A%0A
+    @Autowired%0A    private TypeResolver typeResolver;%0A%0A
     @Bea
@@ -2119,32 +2119,32 @@
 tp%22, %22https%22)))%0A
-
             .api
@@ -2150,32 +2150,442 @@
 iInfo(apiInfo())
+%0A            .alternateTypeRules(AlternateTypeRules.newRule(typeResolver.resolve(Map.class, String.class,%0A            typeResolver.resolve(Map.class, String.class, typeResolver.resolve(List.class, ClinicalDataCount.class))), %0A            typeResolver.resolve(Map.class, String.class, WildcardType.class), Ordered.HIGHEST_PRECEDENCE))%0A            .additionalModels(typeResolver.resolve(ClinicalDataCount.class))
 ;%0A    %7D%0A%0A    @Be

Fix the type error in Swagger