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 java.util.Date;

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

/**
 * 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 MessagesSentEvent(new String[]{msg.id}));
            }
        }

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

    public void sendMessages(Message[] msgs) {
        titan.client.messages.Message[] tmsgs = new titan.client.messages.Message[msgs.length];
        Date now = new Date();
        for (int i = 0; i < msgs.length; i++) {
            tmsgs[i] = new titan.client.messages.Message(null, msgs[i].to, now, msgs[i].body);
        }

        client.sendMessages(tmsgs, new SendMsgCallback() {
            @Override
            public void sentToServer() {
//                eventBus.post(new MessagesSentEvent(msg.id));
            }
        });
    }

    public void echo() {

    }

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

    public class MessagesSentEvent {
        final String[] ids;

        public MessagesSentEvent(String[] ids) {
            this.ids = ids;
        }
    }

    public class MessagesDeliveredEvent {
        final String[] ids;

        public MessagesDeliveredEvent(String[] ids) {
            this.ids = ids;
        }
    }

    public class EchoReceivedEvent {
        public final String message;

        public EchoReceivedEvent(String message) {
            this.message = message;
        }
    }
}

@@ -1189,32 +1189,100 @@
 ssage%5B%5D msgs) %7B%0A
+                // todo: eventBus.post(new MessagesReceivedEvent())%0A
             %7D%0A  
@@ -2534,18 +2534,17 @@
 ver() %7B%0A
-//
+ 
         
@@ -2542,32 +2542,40 @@
                 
+// todo:
  eventBus.post(n
@@ -2742,16 +2742,197 @@
 *****/%0A%0A
+    public class MessagesReceivedEvent %7B%0A        public final Message%5B%5D msgs;%0A%0A        public MessagesReceivedEvent(Message%5B%5D msgs) %7B%0A            this.msgs = msgs;%0A        %7D%0A    %7D%0A%0A
     publ
@@ -2960,32 +2960,39 @@
 Event %7B%0A        
+public 
 final String%5B%5D i
@@ -3132,24 +3132,24 @@
 Event %7B%0A
-
         
 final St
@@ -3140,16 +3140,23 @@
         
+public 
 final St

add Worker.MessagesReceivedEvent