package chat.rocket.android.service.internal;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import chat.rocket.android.api.DDPClientWraper;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.service.Registerable;
import chat.rocket.android.service.ddp.stream.StreamRoomMessage;

/**
 * wrapper for managing stream-notify-message depending on RocketChatCache.
 */
public class StreamRoomMessageManager implements Registerable {
  private StreamRoomMessage streamRoomMessage;

  private final Context context;
  private final RealmHelper realmHelper;
  private final DDPClientWraper ddpClient;
  private final AbstractRocketChatCacheObserver cacheObserver;
  private final Handler handler;

  public StreamRoomMessageManager(Context context, RealmHelper realmHelper,
      DDPClientWraper ddpClient) {
    this.context = context;
    this.realmHelper = realmHelper;
    this.ddpClient = ddpClient;

    cacheObserver = new AbstractRocketChatCacheObserver(context, realmHelper) {
      @Override protected void onRoomIdUpdated(String roomId) {
        unregisterStreamNotifyMessageIfNeeded();
        registerStreamNotifyMessage(roomId);
      }
    };
    handler = new Handler(Looper.myLooper());
  }

  private void registerStreamNotifyMessage(String roomId) {
    streamRoomMessage = new StreamRoomMessage(context, realmHelper, ddpClient, roomId);
    handler.post(() -> {
      streamRoomMessage.register();
    });
  }

  private void unregisterStreamNotifyMessageIfNeeded() {
    if (streamRoomMessage != null) {
      handler.post(() -> {
        streamRoomMessage.unregister();
        streamRoomMessage = null;
      });
    }
  }

  @Override public void register() {
    cacheObserver.register();
  }

  @Override public void unregister() {
    unregisterStreamNotifyMessageIfNeeded();
    cacheObserver.unregister();
  }
}

@@ -1319,24 +1319,51 @@
 g roomId) %7B%0A
+    handler.post(() -%3E %7B%0A  
     streamRo
@@ -1442,33 +1442,8 @@
 d);%0A
-    handler.post(() -%3E %7B%0A
     
@@ -1540,24 +1540,51 @@
 fNeeded() %7B%0A
+    handler.post(() -%3E %7B%0A  
     if (stre
@@ -1612,35 +1612,8 @@
 ) %7B%0A
-      handler.post(() -%3E %7B%0A
     
@@ -1689,24 +1689,24 @@
 %0A      %7D
-);
 %0A    %7D
+);
 %0A  %7D%0A%0A  

fix crash on switching rooms.