package fr.free.nrw.commons.theme;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import fr.free.nrw.commons.AboutActivity;
import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.WelcomeActivity;
import fr.free.nrw.commons.auth.AccountUtil;
import fr.free.nrw.commons.auth.LoginActivity;
import fr.free.nrw.commons.contributions.ContributionsActivity;
import fr.free.nrw.commons.nearby.NearbyActivity;
import fr.free.nrw.commons.settings.SettingsActivity;
import timber.log.Timber;

public abstract class NavigationBaseActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.navigation_view)
    NavigationView navigationView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;

    private ActionBarDrawerToggle toggle;

    public void initDrawer() {
        navigationView.setNavigationItemSelectedListener(this);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.setDrawerIndicatorEnabled(true);
        toggle.syncState();
        setDrawerPaneWidth();
        setUserName();
    }

    /**
     * Set the username in navigationHeader.
     */
    private void setUserName() {

        View navHeaderView = navigationView.getHeaderView(0);
        TextView username = (TextView) navHeaderView.findViewById(R.id.username);

        AccountManager accountManager = AccountManager.get(this);
        Account[] allAccounts = accountManager.getAccountsByType(AccountUtil.accountType());
        if (allAccounts.length != 0) {
            username.setText(allAccounts[0].name);
        }
    }

    public void initBackButton() {
        int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
        toggle.setDrawerIndicatorEnabled(backStackEntryCount == 0);
        toggle.setToolbarNavigationClickListener(v -> onBackPressed());
    }

    public void initBack() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    private void setDrawerPaneWidth() {
        ViewGroup.LayoutParams params = navigationView.getLayoutParams();
        // set width to lowerBound of 80% of the screen size
        params.width = (getResources().getDisplayMetrics().widthPixels * 70) / 100;
        navigationView.setLayoutParams(params);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_home:
                drawerLayout.closeDrawer(navigationView);
                if (!(this instanceof ContributionsActivity)) {
                    ContributionsActivity.startYourself(this);
                }
                return true;
            case R.id.action_nearby:
                drawerLayout.closeDrawer(navigationView);
                if (!(this instanceof NearbyActivity)) {
                    NearbyActivity.startYourself(this);
                }
                return true;
            case R.id.action_about:
                drawerLayout.closeDrawer(navigationView);
                if (!(this instanceof AboutActivity)) {
                    AboutActivity.startYourself(this);
                }
                return true;
            case R.id.action_settings:
                drawerLayout.closeDrawer(navigationView);
                if (!(this instanceof SettingsActivity)) {
                    SettingsActivity.startYourself(this);
                }
                return true;
            case R.id.action_introduction:
                drawerLayout.closeDrawer(navigationView);
                WelcomeActivity.startYourself(this);
                return true;
            case R.id.action_feedback:
                drawerLayout.closeDrawer(navigationView);
                Intent feedbackIntent = new Intent(Intent.ACTION_SEND);
                feedbackIntent.setType("message/rfc822");
                feedbackIntent.putExtra(Intent.EXTRA_EMAIL,
                        new String[]{CommonsApplication.FEEDBACK_EMAIL});
                feedbackIntent.putExtra(Intent.EXTRA_SUBJECT,
                        String.format(CommonsApplication.FEEDBACK_EMAIL_SUBJECT,
                                BuildConfig.VERSION_NAME));
                try {
                    startActivity(feedbackIntent);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(this, R.string.no_email_client, Toast.LENGTH_SHORT).show();
                }
                return true;
            case R.id.action_logout:
                new AlertDialog.Builder(this)
                        .setMessage(R.string.logout_verification)
                        .setCancelable(false)
                        .setPositiveButton(R.string.yes, (dialog, which) -> {
                            BaseLogoutListener logoutListener = new BaseLogoutListener();
                            CommonsApplication app = (CommonsApplication) getApplication();
                            app.clearApplicationData(this, logoutListener);
                        })
                        .setNegativeButton(R.string.no, (dialog, which) -> dialog.cancel())
                        .show();
                return true;
            default:
                return false;
        }
    }

    private class BaseLogoutListener implements CommonsApplication.LogoutListener {
        @Override
        public void onLogoutComplete() {
            Timber.d("Logout complete callback received.");
            Intent nearbyIntent = new Intent(
                    NavigationBaseActivity.this, LoginActivity.class);
            nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(nearbyIntent);
            finish();
        }
    }
}

@@ -3426,32 +3426,67 @@
     
-switch (item.getItemId()
+final int itemId = item.getItemId();%0A        switch (itemId
 ) %7B%0A
@@ -3735,35 +3735,29 @@
             
-return true
+break
 ;%0A          
@@ -3984,35 +3984,29 @@
             
-return true
+break
 ;%0A          
@@ -4230,35 +4230,29 @@
             
-return true
+break
 ;%0A          
@@ -4485,35 +4485,29 @@
             
-return true
+break
 ;%0A          
@@ -4662,35 +4662,29 @@
             
-return true
+break
 ;%0A          
@@ -5491,35 +5491,29 @@
             
-return true
+break
 ;%0A          
@@ -6205,19 +6205,13 @@
     
-return true
+break
 ;%0A  
@@ -6249,31 +6249,166 @@
     
-return false;%0A        %7D
+Timber.e(%22Unknown option %5B%25s%5D selected from the navigation menu%22, itemId);%0A                return false;%0A        %7D%0A        this.finish();%0A        return true;
 %0A   

Fix for same activity getting instantiated multiple times