package io.joynr.joynrandroidruntime;

/*
 * #%L
 * %%
 * Copyright (C) 2011 - 2013 BMW Car IT GmbH
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import io.joynr.arbitration.DiscoveryQos;
import io.joynr.dispatcher.rpc.JoynrInterface;
import io.joynr.exceptions.JoynrArbitrationException;
import io.joynr.exceptions.JoynrIllegalStateException;
import io.joynr.messaging.MessagingQos;
import io.joynr.proxy.ProxyBuilder;
import io.joynr.runtime.JoynrRuntime;

import java.util.concurrent.TimeUnit;

import android.os.AsyncTask;
import android.util.Log;

public class AndroidProxyBuilder<T extends JoynrInterface> extends AsyncTask<Object, String, T> implements
        ProxyBuilder<T> {

    private JoynrRuntime runtime;
    private String providerDomain;
    private MessagingQos messagingQos;
    private DiscoveryQos discoveryQos;
    private UILogger uiLogger;
    private ProxyCreatedCallback<T> callback = null;
    Class<T> proxyInterface;
    private String participantId = null;
    private ProxyBuilder<T> builder = null;
    private InitRuntimeTask runtimeInitTask;

    public AndroidProxyBuilder(InitRuntimeTask runtimeInitTask,
                               String providerDomain,
                               Class<T> proxyInterface,
                               UILogger uiLogger) {
        super();
        this.runtimeInitTask = runtimeInitTask;

        this.providerDomain = providerDomain;
        this.proxyInterface = proxyInterface;
        this.uiLogger = uiLogger;
    }

    @Override
    protected T doInBackground(Object... arg0) {

        Log.d("JAS", "starting CreateProxy");
        return buildProxy();
    }

    private T buildProxy() {
        T proxy = null;
        try {
            this.runtime = runtimeInitTask.get(discoveryQos.getDiscoveryTimeout(), TimeUnit.MILLISECONDS);
            builder = runtime.getProxyBuilder(providerDomain, proxyInterface);
            if (participantId != null) {
                builder.setParticipantId(participantId);
            }
            proxy = builder.setMessagingQos(messagingQos).setDiscoveryQos(discoveryQos).build();
        } catch (JoynrIllegalStateException e) {
            Log.e("JAS", e.toString());
            publishProgress(e.getMessage());
            e.printStackTrace();
        } catch (JoynrArbitrationException e) {
            Log.e("JAS", e.toString());
            publishProgress(e.getMessage());
            e.printStackTrace();
        } catch (InterruptedException e) {
            Log.e("JAS", e.toString());
            publishProgress(e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            if (e.getMessage() != null) {
                Log.e("JAS", e.toString());
                publishProgress(e.getMessage());
            }
            e.printStackTrace();
        }
        Log.d("JAS", "Returning Proxy");
        return proxy;
    }

    @Override
    protected void onProgressUpdate(String... progress) {
        uiLogger.logText(progress);
    }

    @Override
    protected void onPostExecute(T result) {
        Log.d("JAS", "calling onProxyCreated Callback");
        callback.onProxyCreated(result);
    }

    @Override
    public String getParticipantId() {
        if (builder != null) {
            participantId = builder.getParticipantId();
        }
        return participantId;
    }

    @Override
    public void setParticipantId(String participantId) {
        this.participantId = participantId;
        if (builder != null) {
            builder.setParticipantId(participantId);
        }

    }

    @Override
    public ProxyBuilder<T> setDiscoveryQos(DiscoveryQos discoveryQos) throws JoynrArbitrationException {
        this.discoveryQos = discoveryQos;
        return this;
    }

    @Override
    public ProxyBuilder<T> setMessagingQos(MessagingQos messagingQos) {
        this.messagingQos = messagingQos;
        return this;
    }

    @Override
    public T build() {
        // TODO test if this works
        return buildProxy();
    }

    @Override
    public void build(ProxyCreatedCallback<T> newCallback) {
        this.callback = newCallback;
        this.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    }
}

@@ -816,63 +816,8 @@
 on;%0A
-import io.joynr.exceptions.JoynrIllegalStateException;%0A
 impo
@@ -955,24 +955,118 @@
 current.
-TimeUnit
+ExecutionException;%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.concurrent.TimeoutException
 ;%0A%0Aimpor
@@ -2167,32 +2167,50 @@
  CreateProxy%22);%0A
+        try %7B%0A    
         return b
@@ -2230,82 +2230,337 @@
     
-%7D%0A%0A    private T buildProxy() %7B%0A        T proxy = null;%0A        try %7B%0A    
+    %7D catch (InterruptedException %7C ExecutionException %7C TimeoutException e) %7B%0A            if (callback != null) %7B%0A                callback.onProxyCreationError(e.getMessage());%0A            %7D%0A            return null;%0A        %7D%0A    %7D%0A%0A    private T buildProxy() throws InterruptedException, ExecutionException, TimeoutException %7B%0A
     
@@ -2662,28 +2662,24 @@
 S);%0A        
-   
- 
 builder = ru
@@ -2737,28 +2737,24 @@
 e);%0A        
-    
 if (particip
@@ -2774,36 +2774,32 @@
 ) %7B%0A            
-   
- 
 builder.setParti
@@ -2823,36 +2823,32 @@
 antId);%0A        
-    
 %7D%0A            pr
@@ -2841,19 +2841,17 @@
         
-   
+T
  proxy =
@@ -2932,726 +2932,8 @@
 ();%0A
-        %7D catch (JoynrIllegalStateException e) %7B%0A            Log.e(%22JAS%22, e.toString());%0A            publishProgress(e.getMessage());%0A            e.printStackTrace();%0A        %7D catch (JoynrArbitrationException e) %7B%0A            Log.e(%22JAS%22, e.toString());%0A            publishProgress(e.getMessage());%0A            e.printStackTrace();%0A        %7D catch (InterruptedException e) %7B%0A            Log.e(%22JAS%22, e.toString());%0A            publishProgress(e.getMessage());%0A            e.printStackTrace();%0A        %7D catch (Exception e) %7B%0A            if (e.getMessage() != null) %7B%0A                Log.e(%22JAS%22, e.toString());%0A                publishProgress(e.getMessage());%0A            %7D%0A            e.printStackTrace();%0A        %7D%0A
     
@@ -3168,24 +3168,78 @@
 T result) %7B%0A
+        if (result != null && callback != null) %7B%0A    
         Log.
@@ -3283,32 +3283,36 @@
 back%22);%0A        
+    
 callback.onProxy
@@ -3328,16 +3328,26 @@
 esult);%0A
+        %7D%0A
     %7D%0A%0A 
@@ -4143,61 +4143,151 @@
     
-// TODO test if this works%0A        return
+throw new UnsupportedOperationException(%22On Android, only method signature public void
  build
+(
 Proxy
-(
+CreatedCallback%3CT%3E newCallback) is supported%22
 );%0A 

Use callback.onProxyCreationError in AndroidProxyBuilder