/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2016,2017  
 */
package com.ibm.streamsx.topology.internal.streaminganalytics;

import static com.ibm.streamsx.topology.context.AnalyticsServiceProperties.SERVICE_NAME;
import static com.ibm.streamsx.topology.context.AnalyticsServiceProperties.VCAP_SERVICES;
import static com.ibm.streamsx.topology.internal.gson.GsonUtilities.array;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.ibm.streamsx.topology.function.Function;

/**
 * Utilities to get the correct VCAP services
 * information for submission to Streaming Analytics Service.
 *
 */
public class VcapServices {
    
    /**
     * Get the top-level VCAP services object.
     * 
     * Object can be:
     * JsonObject - assumed to contain VCAP_SERVICES
     * String -  assumed to contain serialized VCAP_SERVICES JSON
     * File - assumed to be a file containing serialized VCAP_SERVICES JSON
     */
    private static JsonObject getVCAPServices(Object rawServices) throws IOException {
        
        if (rawServices != null)
        System.out.println("rawServicesClass:" + rawServices.getClass());
        System.out.println("rawServices:" + rawServices);

        if (rawServices instanceof JsonObject)
            return (JsonObject) rawServices;
        
        JsonParser parser = new JsonParser();
        
        String vcapString;
        if (rawServices instanceof File) {
            File fServices = (File) rawServices;          
            vcapString = new String(Files.readAllBytes(fServices.toPath()), StandardCharsets.UTF_8);

        } else if (rawServices instanceof JsonPrimitive) {
            vcapString = ((JsonPrimitive) rawServices).getAsString();
        }
        else if (rawServices instanceof String) {
            vcapString = rawServices.toString();
            
        } else {
            throw new IllegalArgumentException();
        }
        
        return parser.parse(vcapString).getAsJsonObject();
    }
    
    /**
     * Get the sepcific streaming analytics service from the service name
     * and the vcap services.
     * @param getter How to get the value from the container given a key

     * @throws IOException
     */
    public static JsonObject getVCAPService(Function<String,Object> getter) throws IOException {
        JsonObject services = getVCAPServices(getter.apply(VCAP_SERVICES));
        
        JsonArray streamsServices = array(services, "streaming-analytics");
        if (streamsServices == null || streamsServices.size() == 0)
            throw new IllegalStateException("No streaming-analytics services defined in VCAP_SERVICES");
        
        final String serviceName;
        Object sno = getter.apply(SERVICE_NAME);
        if (sno instanceof JsonPrimitive)
            serviceName = ((JsonPrimitive) sno).getAsString();
        else
            serviceName = sno.toString();
                
        JsonObject service = null;
        for (JsonElement ja : streamsServices) {
            JsonObject possibleService = ja.getAsJsonObject();
            if (serviceName.equals(possibleService.get("name").getAsString())) {
                service = possibleService;
                break;
            }
        }
        if (service == null)
            throw new IllegalStateException(
                    "No streaming-analytics services defined in VCAP_SERVICES with name: " + serviceName);
        
        return service;
    }
}

@@ -2912,14 +2912,8 @@
     
-final 
 Stri
@@ -2928,16 +2928,16 @@
 ceName;%0A
+
         
@@ -3129,32 +3129,83 @@
 sno.toString();%0A
+        %0A        serviceName = serviceName.trim();%0A
                 
@@ -3240,16 +3240,58 @@
 = null;%0A
+        if (!serviceName.isEmpty()) %7B%0A    
         
@@ -3327,24 +3327,28 @@
 Services) %7B%0A
+    
             
@@ -3402,32 +3402,36 @@
 ();%0A            
+  
+  
 if (serviceName.
@@ -3495,24 +3495,28 @@
             
+    
 service = po
@@ -3550,15 +3550,37 @@
     
+   
+ 
 break;%0A
+                %7D%0A
     

Trim service name