package com.nbusy.app;

import android.os.AsyncTask;

import com.google.common.eventbus.EventBus;
import com.nbusy.sdk.Client;
import com.nbusy.sdk.ClientImpl;

import neptulon.client.callbacks.ConnCallback;
import titan.client.callbacks.RecvMsgsCallback;

/**
 * Manages persistent connection to NBusy servers and the persistent queue for relevant operations.
 * All notifications from this class is sent out using an event bus.
 */
public class Worker {
    private static final String TAG = Worker.class.getSimpleName();
    private final Client client;
    private final EventBus eventBus;

    public Worker(Client client, EventBus eventBus) {
        this.client = client;
        this.eventBus = eventBus;
        client.connect(new ConnCallback() {
            @Override
            public void connected() {

            }

            @Override
            public void disconnected(String reason) {

            }
        });
    }

    public Worker() {
        this(new ClientImpl("ws://10.0.0.2:3001", new RecvMsgsCallback() {
            @Override
            public void callback(titan.client.messages.Message[] msgs) {
            }
        }), new EventBus(TAG));
    }

    public EventBus getEventBus() {
        return eventBus;
    }

    public void sendMessage(final Message msg) {
        //client.send(msg, callback)
        class SimulateClient extends AsyncTask<Object, Object, Object> {
            @Override
            protected Object doInBackground(Object[] params) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Object o) {
                eventBus.post(new MessageSentEvent(msg.id));
            }
        }

        new SimulateClient().execute(null, null, null);
    }

    /*****************
     * Event Objects *
     *****************/

    public class MessageSentEvent {
        final String id;

        public MessageSentEvent(String id) {
            this.id = id;
        }
    }

    public class MessageDeliveredEvent {
        final String id;

        public MessageDeliveredEvent(String id) {
            this.id = id;
        }
    }
}

@@ -806,25 +806,24 @@
 nnected() %7B%0A
-%0A
             
@@ -901,17 +901,16 @@
 ason) %7B%0A
-%0A
         
@@ -1926,24 +1926,57 @@
 ll);%0A    %7D%0A%0A
+    public void echo() %7B%0A%0A    %7D%0A%0A
     /*******
@@ -2342,11 +2342,180 @@
  %7D%0A    %7D
+%0A%0A    public class EchoResponse %7B%0A        public final String message;%0A%0A        public EchoResponse(String message) %7B%0A            this.message = message;%0A        %7D%0A    %7D
 %0A%7D%0A

add Worker.echo stub