/*
 * Licensed Materials - Property of IBM
 * © Copyright IBM Corporation 2015. All Rights Reserved.
 */

package com.ibm.mil.cafejava;

import android.content.Context;
import android.util.Log;

import com.worklight.wlclient.api.WLClient;
import com.worklight.wlclient.api.WLFailResponse;
import com.worklight.wlclient.api.WLRequestOptions;
import com.worklight.wlclient.api.WLResponse;
import com.worklight.wlclient.api.WLResponseListener;

import rx.Observable;
import rx.Subscriber;

public final class CafeJava {
    private static final String TAG = CafeJava.class.getName();
    private static final int DEFAULT_TIMEOUT = 30_000;

    private Object[] parameters = new Object[] {};
    private int timeout = DEFAULT_TIMEOUT;
    private Object invocationContext;

    public <T> Observable<T> createProcedureObservable(String adapterName, String procedureName) {
        return Observable.create(new Observable.OnSubscribe<T>() {
            @Override public void call(Subscriber<? super T> subscriber) {
                Log.i(TAG, "createProduceObservable called");
            }
        });
    }

    public <T> Observable<T> createConnectionObservable(final Context context) {
        return Observable.create(new Observable.OnSubscribe<T>() {
            @Override public void call(final Subscriber<? super T> subscriber) {
                Log.i(TAG, "createConnectionObservable called");

                WLRequestOptions requestOptions = new WLRequestOptions();
                requestOptions.setTimeout(timeout);
                requestOptions.setInvocationContext(invocationContext);

                WLClient client = WLClient.createInstance(context);
                client.connect(new WLResponseListener() {
                    @Override public void onSuccess(WLResponse wlResponse) {
                        subscriber.onNext(null);
                        subscriber.onCompleted();
                    }

                    @Override public void onFailure(WLFailResponse wlFailResponse) {
                        subscriber.onError(new Throwable(wlFailResponse.getErrorMsg()));
                    }
                }, requestOptions);
            }
        });
    }

    public CafeJava parameters(Object... parameters) {
        this.parameters = parameters;
        return this;
    }

    public CafeJava timeout(int timeout) {
        this.timeout = timeout;
        return this;
    }

    public CafeJava invocationContext(Object invocationContext) {
        this.invocationContext = invocationContext;
        return this;
    }

}

@@ -274,32 +274,93 @@
 WLFailResponse;%0A
+import com.worklight.wlclient.api.WLProcedureInvocationData;%0A
 import com.workl
@@ -741,16 +741,67 @@
 ct%5B%5D %7B%7D;
+ // TODO: make varargs to createProduceObservable()
 %0A    pri
@@ -875,16 +875,68 @@
 ntext;%0A%0A
+    // TODO: deal with raw WL response and generics%0A
     publ
@@ -982,16 +982,22 @@
 ervable(
+final 
 String a
@@ -1007,16 +1007,22 @@
 terName,
+ final
  String 
@@ -1144,16 +1144,22 @@
 id call(
+final 
 Subscrib
@@ -1247,16 +1247,1001 @@
 alled%22);
+%0A%0A                WLClient client = WLClient.getInstance();%0A                if (client == null) %7B%0A                    subscriber.onError(new Throwable(%22WLClient instance does not exist%22));%0A                    return;%0A                %7D%0A%0A                // TODO: encode params and deal with compression argument%0A                WLProcedureInvocationData invocationData =%0A                        new WLProcedureInvocationData(adapterName, procedureName, false);%0A                invocationData.setParameters(parameters);%0A%0A                client.invokeProcedure(invocationData, new WLResponseListener() %7B%0A                    @Override public void onSuccess(WLResponse wlResponse) %7B%0A                        subscriber.onNext(null);%0A                    %7D%0A%0A                    @Override public void onFailure(WLFailResponse wlFailResponse) %7B%0A                        subscriber.onError(new Throwable(wlFailResponse.getErrorMsg()));%0A                    %7D%0A                %7D, getRequestOptions());
 %0A       
@@ -2565,207 +2565,8 @@
 );%0A%0A
-                WLRequestOptions requestOptions = new WLRequestOptions();%0A                requestOptions.setTimeout(timeout);%0A                requestOptions.setInvocationContext(invocationContext);%0A%0A
     
@@ -3101,17 +3101,20 @@
      %7D, 
-r
+getR
 equestOp
@@ -3118,16 +3118,18 @@
 tOptions
+()
 );%0A     
@@ -3526,10 +3526,273 @@
 %0A    %7D%0A%0A
+    private WLRequestOptions getRequestOptions() %7B%0A        WLRequestOptions requestOptions = new WLRequestOptions();%0A        requestOptions.setTimeout(timeout);%0A        requestOptions.setInvocationContext(invocationContext);%0A        return requestOptions;%0A    %7D%0A%0A
 %7D%0A

Implement createProcedureObservable