/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2015  
 */
package com.ibm.streamsx.topology.internal.context.streams;

import static com.ibm.streamsx.topology.context.ContextProperties.FORCE_REMOTE_BUILD;
import static com.ibm.streamsx.topology.internal.context.remote.DeployKeys.deploy;
import static com.ibm.streamsx.topology.internal.context.remote.DeployKeys.keepArtifacts;
import static com.ibm.streamsx.topology.internal.context.remote.RemoteBuildAndSubmitRemoteContext.streamingAnalyticServiceFromDeploy;
import static com.ibm.streamsx.topology.internal.gson.GsonUtilities.jboolean;
import static com.ibm.streamsx.topology.internal.streaminganalytics.VcapServices.getVCAPService;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.concurrent.Future;

import com.google.gson.JsonObject;
import com.ibm.streamsx.rest.Job;
import com.ibm.streamsx.rest.Result;
import com.ibm.streamsx.rest.StreamingAnalyticsService;
import com.ibm.streamsx.topology.context.ContextProperties;
import com.ibm.streamsx.topology.context.remote.RemoteContext;
import com.ibm.streamsx.topology.internal.context.JSONStreamsContext.AppEntity;
import com.ibm.streamsx.topology.internal.context.remote.DeployKeys;
import com.ibm.streamsx.topology.internal.context.remote.RemoteContexts;
import com.ibm.streamsx.topology.internal.context.remote.SubmissionResultsKeys;
import com.ibm.streamsx.topology.internal.context.service.RemoteStreamingAnalyticsServiceStreamsContext;
import com.ibm.streamsx.topology.internal.gson.GsonUtilities;
import com.ibm.streamsx.topology.internal.process.CompletedFuture;

public class AnalyticsServiceStreamsContext extends
        BundleUserStreamsContext<BigInteger> {

    private final Type type;
    
    public AnalyticsServiceStreamsContext(Type type) {
        super(false);
        this.type = type;
    }

    @Override
    public Type getType() {
        return type;
    }
    
    @Override
    protected Future<BigInteger> action(AppEntity entity) throws Exception {
        if (jboolean(deploy(entity.submission), FORCE_REMOTE_BUILD)) {
            RemoteStreamingAnalyticsServiceStreamsContext rc = new RemoteStreamingAnalyticsServiceStreamsContext();
            return rc.submit(entity.submission);
        }

        return super.action(entity);
    }
    
    
    @Override
    Future<BigInteger> invoke(AppEntity entity, File bundle) throws Exception {
        try {           
            BigInteger jobId = submitJobToService(bundle, entity.submission);
         
            return new CompletedFuture<BigInteger>(jobId);
        } finally {
            if (!keepArtifacts(entity.submission))
                bundle.delete();
        }
    }
    
    /**
     * Verify we have a valid Streaming Analytic service
     * information before we attempt anything.
     */
    @Override
    protected void preSubmit(AppEntity entity) {
        
            
        try {
            if (entity.submission != null)
                getVCAPService(deploy(entity.submission));
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    } 
    
    /*
   
    private JsonObject getBluemixSubmitConfig( Map<String, Object> config) throws IOException {
        
        JobConfig jc = JobConfig.fromProperties(config);
        
        // Streaming Analytics service is always using 4.2 or later
        // so use the job config overlay
            
        JobConfigOverlay jco = new JobConfigOverlay(jc);
        
        return jco.fullOverlayAsJSON(new JsonObject());
    }
    */

    private BigInteger submitJobToService(File bundle, JsonObject submission) throws IOException {
        JsonObject deploy =  deploy(submission);
        JsonObject jco = DeployKeys.copyJobConfigOverlays(deploy);

        final StreamingAnalyticsService sas = streamingAnalyticServiceFromDeploy(deploy); 
        
        RemoteContexts.checkServiceRunning(sas);

        Result<Job, JsonObject> submitResult = sas.submitJob(bundle, jco);
        final JsonObject submissionResult = GsonUtilities.objectCreate(submission,
                RemoteContext.SUBMISSION_RESULTS);
        GsonUtilities.addAll(submissionResult, submitResult.getRawResult());
        
        // Ensure job id is in a known place regardless of version
        final String jobId = submitResult.getId();
        GsonUtilities.addToObject(submissionResult, SubmissionResultsKeys.JOB_ID, jobId);

        return new BigInteger(jobId);
    }
}

@@ -1020,168 +1020,28 @@
 ext.
-ContextProperties;%0Aimport com.ibm.streamsx.topology.context.remote.RemoteContext;%0Aimport com.ibm.streamsx.topology.internal.context.JSONStreamsContext.AppEntity
+remote.RemoteContext
 ;%0Aim
@@ -1920,62 +1920,29 @@
 if (
-jboolean(deploy(entity.submission), FORCE_REMOTE_BUILD
+useRemoteBuild(entity
 )) %7B
@@ -2162,24 +2162,494 @@
 %0A    %7D%0A    %0A
+    /**%0A     * See if the remote build service should be used.%0A     * If STREAMS_INSTALL was not set is handled elsewhere,%0A     * so this path assumes that STREAMS_INSTALL is set and not empty.%0A     * %0A     * Remote build if:%0A     *  - FORCE_REMOTE_BUILD is set to true.%0A     */%0A    private boolean useRemoteBuild(AppEntity entity) %7B%0A        if (jboolean(deploy(entity.submission), FORCE_REMOTE_BUILD))%0A            return true;%0A        %0A        return false;%0A    %7D%0A    %0A
     %0A    @Ov
@@ -3452,458 +3452,13 @@
   %7D%0A
+
     %7D
- %0A    %0A    /*%0A   %0A    private JsonObject getBluemixSubmitConfig( Map%3CString, Object%3E config) throws IOException %7B%0A        %0A        JobConfig jc = JobConfig.fromProperties(config);%0A        %0A        // Streaming Analytics service is always using 4.2 or later%0A        // so use the job config overlay%0A            %0A        JobConfigOverlay jco = new JobConfigOverlay(jc);%0A        %0A        return jco.fullOverlayAsJSON(new JsonObject());%0A    %7D%0A    */
 %0A%0A  

Move check to method