package it.smartcommunitylab.aac.apimanager;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;

import it.smartcommunitylab.aac.wso2.model.API;
import it.smartcommunitylab.aac.wso2.services.APIPublisherService;

@Component
public class CoreAPIsPublisher {

	@Autowired
	private APIPublisherService pub;
	@Autowired
	private APIProviderManager providerManager;
	
	 @Autowired
	 private ConfigurableEnvironment env;

	@Value("${admin.password}")
	private String adminPassword;

	private final static String ENDPOINT_CONFIG = "{\"production_endpoints\":{\"url\":\"${application.internalUrl}\",\"config\":null},\"sandbox_endpoints\":{\"url\":\"${application.internalUrl}\",\"config\":null},\"endpoint_type\":\"http\"}";

	public void init() throws Exception {
		String token = providerManager.createToken("admin", adminPassword);
		
		publishAPI("api/profile-api.json", "AAC", "AAC User Profile APIs", "/aacprofile", token);
		publishAPI("api/roles-api.json", "AACRoles", "AAC User Roles APIs", "/aacroles", token);
		publishAPI("api/authorization-api.json", "AACAuthorization", "AAC Authorization APIs", "/aacauthorization", token);
	}
	
	@SuppressWarnings("rawtypes")
	private void publishAPI(String json, String name, String description, String context, String token) throws IOException{
		Map apis = pub.findAPI(name, token);

		List list = (List) apis.get("list");
//		System.err.println(apis);
		if (list.isEmpty() || !list.stream().anyMatch(x -> name.equals(((Map)x).get("name")))) {
			ObjectMapper mapper = new ObjectMapper();

			String swagger = Resources.toString(Resources.getResource(json), Charsets.UTF_8);
			swagger = env.resolvePlaceholders(swagger);			
			
			API api = new API();

			api.setName(name);
			api.setDescription(description);
			api.setContext(context);
			api.setVersion("1.0.0");
			api.setProvider("admin");
			api.setApiDefinition(swagger);
			api.setStatus("CREATED");
			api.setVisibility("PUBLIC");
			api.setSubscriptionAvailability("all_tenants");

			api.setIsDefaultVersion(true);
			api.setEndpointConfig(env.resolvePlaceholders(ENDPOINT_CONFIG));			
			
			API result = pub.publishAPI(api, token);
			pub.changeAPIStatus(result.getId(), "Publish", token);
//			System.err.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result));
		}
	}

}

@@ -113,16 +113,101 @@
 l.Map;%0A%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import o
@@ -523,16 +523,56 @@
 arsets;%0A
+import com.google.common.collect.Lists;%0A
 import c
@@ -651,16 +651,78 @@
 el.API;%0A
+import it.smartcommunitylab.aac.wso2.model.CorsConfiguration;%0A
 import i
@@ -2555,17 +2555,16 @@
 ants%22);%0A
-%0A
 %09%09%09api.s
@@ -2660,24 +2660,468 @@
 G));%09%09%09%0A%09%09%09%0A
+%09%09%09CorsConfiguration cors = new CorsConfiguration();%0A%09%09%09cors.setCorsConfigurationEnabled(true);%0A%09%09%09cors.setAccessControlAllowOrigins(Lists.newArrayList(%22*%22));%0A%09%09%09cors.setAccessControlAllowHeaders(Lists.newArrayList(%22authorization%22, %22Access-Control-Allow-Origin%22, %22Content-Type%22, %22SOAPAction%22));%0A%09%09%09cors.setAccessControlAllowMethods(Lists.newArrayList(%22GET%22, %22PUT%22, %22POST%22, %22DELETE%22, %22PATCH%22, %22OPTIONS%22));%0A%09%09%09api.setCorsConfiguration(cors);%0A%09%09%09%0A
 %09%09%09API resul
@@ -3210,16 +3210,20 @@
 token);%0A
+%09%09%09%0A
 //%09%09%09Sys

Enable cors.