package org.wikipedia.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;

import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

import org.wikipedia.R;
import org.wikipedia.WikipediaApp;
import org.wikipedia.activity.SingleFragmentToolbarActivity;
import org.wikipedia.events.ThemeChangeEvent;
import org.wikipedia.navtab.NavTab;
import org.wikipedia.util.log.L;

public class MainActivity extends SingleFragmentToolbarActivity<MainFragment>
        implements MainFragment.Callback {

    private WikipediaApp app;
    private Bus bus;
    private MainActivity.EventBusMethods busMethods;

    public static Intent newIntent(@NonNull Context context) {
        return new Intent(context, MainActivity.class);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (WikipediaApp) getApplicationContext();
        busMethods = new EventBusMethods();
        registerBus();
    }

    @Override protected MainFragment createFragment() {
        return MainFragment.newInstance();
    }

    @Override
    public void onTabChanged(@NonNull NavTab tab) {
        if (tab.equals(NavTab.EXPLORE)) {
            getToolbarWordmark().setVisibility(View.VISIBLE);
            getSupportActionBar().setTitle("");
        } else {
            getToolbarWordmark().setVisibility(View.GONE);
            getSupportActionBar().setTitle(tab.text());
        }
    }

    @Override
    public void onSearchOpen() {
        getToolbar().setVisibility(View.GONE);
        setStatusBarColor(android.R.color.black);
    }

    @Override
    public void onSearchClose(boolean shouldFinishActivity) {
        getToolbar().setVisibility(View.VISIBLE);
        setStatusBarColor(R.color.dark_blue);
        if (shouldFinishActivity) {
            finish();
        }
    }

    @NonNull
    @Override
    public View getOverflowMenuAnchor() {
        View view = getToolbar().findViewById(R.id.menu_overflow_button);
        return view == null ? getToolbar() : view;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        getFragment().handleIntent(intent);
    }

    @Override
    public void onBackPressed() {
        if (getFragment().onBackPressed()) {
            return;
        }
        finish();
    }

    @Override
    public void onDestroy() {
        unregisterBus();
        super.onDestroy();
    }

    private void registerBus() {
        bus = app.getBus();
        bus.register(busMethods);
        L.d("Registered bus.");
    }

    private void unregisterBus() {
        bus.unregister(busMethods);
        bus = null;
        L.d("Unregistered bus.");
    }

    private class EventBusMethods {

        @Subscribe
        public void onChangeTheme(ThemeChangeEvent event) {
            MainActivity.this.recreate();
        }
    }
}

@@ -89,34 +89,8 @@
 nt;%0A
-import android.os.Bundle;%0A
 impo
@@ -166,125 +166,23 @@
 ort 
-com.squareup.otto.Bus;%0Aimport com.squareup.otto.Subscribe;%0A%0Aimport org.wikipedia.R;%0Aimport org.wikipedia.WikipediaApp
+org.wikipedia.R
 ;%0Aim
@@ -244,54 +244,8 @@
 ty;%0A
-import org.wikipedia.events.ThemeChangeEvent;%0A
 impo
@@ -279,41 +279,8 @@
 Tab;
-%0Aimport org.wikipedia.util.log.L;
 %0A%0Apu
@@ -403,113 +403,8 @@
  %7B%0A%0A
-    private WikipediaApp app;%0A    private Bus bus;%0A    private MainActivity.EventBusMethods busMethods;%0A%0A
     
@@ -529,248 +529,8 @@
  %7D%0A%0A
-    @Override%0A    public void onCreate(Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A        app = (WikipediaApp) getApplicationContext();%0A        busMethods = new EventBusMethods();%0A        registerBus();%0A    %7D%0A%0A
     
@@ -1922,551 +1922,7 @@
    %7D
-%0A%0A    @Override%0A    public void onDestroy() %7B%0A        unregisterBus();%0A        super.onDestroy();%0A    %7D%0A%0A    private void registerBus() %7B%0A        bus = app.getBus();%0A        bus.register(busMethods);%0A        L.d(%22Registered bus.%22);%0A    %7D%0A%0A    private void unregisterBus() %7B%0A        bus.unregister(busMethods);%0A        bus = null;%0A        L.d(%22Unregistered bus.%22);%0A    %7D%0A%0A    private class EventBusMethods %7B%0A%0A        @Subscribe%0A        public void onChangeTheme(ThemeChangeEvent event) %7B%0A            MainActivity.this.recreate();%0A        %7D%0A    %7D
 %0A%7D%0A

remove redundant theme change subscriber in MainActivity