package chat.rocket.android.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SlidingPaneLayout;
import android.view.View;
import chat.rocket.android.LaunchUtil;
import chat.rocket.android.R;
import chat.rocket.android.fragment.chatroom.HomeFragment;
import chat.rocket.android.fragment.chatroom.RoomFragment;
import chat.rocket.android.fragment.sidebar.SidebarMainFragment;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmStore;
import hugo.weaving.DebugLog;

/**
 * Entry-point for Rocket.Chat.Android application.
 */
public class MainActivity extends AbstractAuthedActivity {
  @Override protected int getLayoutContainerForFragment() {
    return R.id.activity_main_container;
  }


  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setupSidebar();
    if (roomId == null) {
      showFragment(new HomeFragment());
    }
  }

  private void setupSidebar() {
    SlidingPaneLayout pane = (SlidingPaneLayout) findViewById(R.id.sliding_pane);
    if (pane != null) {
      final SlidingPaneLayout subPane = (SlidingPaneLayout) findViewById(R.id.sub_sliding_pane);
      pane.setPanelSlideListener(new SlidingPaneLayout.SimplePanelSlideListener() {
        @Override public void onPanelClosed(View panel) {
          super.onPanelClosed(panel);
          if (subPane != null) {
            subPane.closePane();
          }
        }
      });
    }
  }

  private void closeSidebarIfNeeded() {
    // REMARK: Tablet UI doesn't have SlidingPane!
    SlidingPaneLayout pane = (SlidingPaneLayout) findViewById(R.id.sliding_pane);
    if (pane != null) {
      pane.closePane();
    }
  }

  @DebugLog
  @Override protected void onServerConfigIdUpdated() {
    super.onServerConfigIdUpdated();

    updateSidebarMainFragment();
    showServerConfigActivityIfNeeded();
  }

  private void updateSidebarMainFragment() {
    getSupportFragmentManager().beginTransaction()
        .replace(R.id.sidebar_fragment_container, SidebarMainFragment.create(serverConfigId))
        .commit();
  }

  private void showServerConfigActivityIfNeeded() {
    if (serverConfigId == null) {
      return;
    }

    RealmHelper realmHelper = RealmStore.get(serverConfigId);
    if (realmHelper == null) {
      return;
    }

    Session session = realmHelper.executeTransactionForRead(realm ->
        realm.where(Session.class).equalTo("sessionId", Session.DEFAULT_ID).findFirst());

    if (session != null
        && !TextUtils.isEmpty(session.getToken())
        && session.isTokenVerified()
        && TextUtils.isEmpty(session.getError())) {
      // session is OK.
    } else {
      LaunchUtil.showServerConfigActivity(this, serverConfigId);
    }
  }

  @Override protected void onRoomIdUpdated() {
    super.onRoomIdUpdated();

    if (roomId != null) {
      showFragment(RoomFragment.create(serverConfigId, roomId));
      closeSidebarIfNeeded();
    } else {
      showFragment(new HomeFragment());
    }
  }


}

@@ -1681,20 +1681,23 @@
 private 
-void
+boolean
  closeSi
@@ -1859,32 +1859,49 @@
 if (pane != null
+ && pane.isOpen()
 ) %7B%0A      pane.c
@@ -1912,25 +1912,62 @@
 Pane();%0A    
-%7D
+  return true;%0A    %7D%0A    return false;
 %0A  %7D%0A%0A  @Deb
@@ -3276,11 +3276,118 @@
  %7D%0A  %7D%0A%0A
+  @Override protected boolean onBackPress() %7B%0A    return closeSidebarIfNeeded() %7C%7C super.onBackPress();%0A  %7D
 %0A%7D%0A

fix the behavior of back-press on SlidingPane is opened.