package org.wikipedia;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import org.wikipedia.history.HistoryActivity;
import org.wikipedia.login.LoginActivity;
import org.wikipedia.login.LogoutTask;
import org.wikipedia.savedpages.SavedPagesActivity;
import org.wikipedia.settings.SettingsActivity;

public class NavDrawerFragment extends Fragment implements AdapterView.OnItemClickListener {
    private static final int[] ACTION_ITEMS_TEXT = {
            R.string.nav_item_history,
            R.string.nav_item_saved_pages,
            R.string.nav_item_preferences,
            R.string.nav_item_login
    };
    private static final int[] ACTION_ITEM_IMAGES = {
            android.R.drawable.ic_menu_recent_history,
            android.R.drawable.ic_menu_save,
            android.R.drawable.ic_menu_preferences,
            android.R.drawable.ic_menu_add
    };

    private ListView navList;
    private NavListAdapter adapter;
    private WikipediaApp app;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View parentView = inflater.inflate(R.layout.fragment_navdrawer, container, false);
        navList = (ListView) parentView.findViewById(R.id.nav_list);
        adapter = new NavListAdapter();
        app = (WikipediaApp)getActivity().getApplicationContext();

        setupDynamicItems();

        navList.setAdapter(adapter);
        navList.setOnItemClickListener(this);

        return parentView;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        switch ((Integer)view.getTag()) {
            case R.string.nav_item_history:
                intent.setClass(this.getActivity(), HistoryActivity.class);
                getActivity().startActivity(intent);
                break;
            case R.string.nav_item_saved_pages:
                intent.setClass(this.getActivity(), SavedPagesActivity.class);
                startActivity(intent);
                break;
            case R.string.nav_item_preferences:
                intent.setClass(this.getActivity(), SettingsActivity.class);
                startActivity(intent);
                break;
            case R.string.nav_item_login:
                intent.setClass(this.getActivity(), LoginActivity.class);
                startActivityForResult(intent, LoginActivity.REQUEST_CODE_LOGIN);
                break;
            case R.string.nav_item_logout:
                doLogout();
                break;
            default:
                throw new RuntimeException("Unknown ID clicked!");
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == LoginActivity.LOG_IN_SUCCESSFUL) {
            setupDynamicItems();
            ((NavListAdapter)navList.getAdapter()).notifyDataSetChanged();
        }
    }

    private void setupDynamicItems() {
        // Do login / logout swap
        if (app.getUserInfoStorage().isLoggedIn()) {
            ACTION_ITEMS_TEXT[3] = R.string.nav_item_logout;
        } else {
            ACTION_ITEMS_TEXT[3] = R.string.nav_item_login;
        }
    }

    private void doLogout() {
        final ProgressDialog progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage(getString(R.string.logging_out_progress));
        progressDialog.setIndeterminate(true);

        new LogoutTask(app, app.getPrimarySite()) {
            @Override
            public void onBeforeExecute() {
                progressDialog.show();
            }

            @Override
            public void onFinish(Boolean result) {
                progressDialog.dismiss();
                setupDynamicItems();
                ((NavListAdapter)navList.getAdapter()).notifyDataSetChanged();
            }
        }.execute();
    }

    private class NavListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return ACTION_ITEMS_TEXT.length;
        }

        @Override
        public Object getItem(int position) {
            return ACTION_ITEMS_TEXT[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.item_nav_item, parent, false);
            }
            TextView navText = (TextView)convertView.findViewById(R.id.nav_item_text);
            ImageView navImage = (ImageView)convertView.findViewById(R.id.nav_item_image);
            navText.setText(ACTION_ITEMS_TEXT[position]);
            navImage.setImageResource(ACTION_ITEM_IMAGES[position]);
            convertView.setTag(ACTION_ITEMS_TEXT[position]);

            return convertView;
        }
    }
}
@@ -1293,25 +1293,14 @@
     
-View parentView =
+return
  inf
@@ -1353,32 +1353,169 @@
 tainer, false);%0A
+    %7D%0A%0A    @Override%0A    public void onActivityCreated(Bundle savedInstanceState) %7B%0A        super.onActivityCreated(savedInstanceState);%0A
         navList 
@@ -1527,26 +1527,25 @@
 stView) 
-paren
+ge
 tView
+()
 .findVie
@@ -1790,36 +1790,8 @@
 is);
-%0A%0A        return parentView;
 %0A   
@@ -3058,24 +3058,501 @@
 ent data) %7B%0A
+        // Okay, so this is really, really stupid, but%0A        // sometimes if the previous activity was destroyed before the callback is done%0A        // onActivityResult may be called *before* the onCreate, onActivityAttach, etc are called%0A        // This, of course, is fucking stupid. However, in this particular case (updating the login status)%0A        // we can just ignore it if that is the case and keep going.%0A        // BUGS! GRR!%0A        if (getView() != null) %7B%0A    
         if (
@@ -3604,32 +3604,36 @@
 ) %7B%0A            
+    
 setupDynamicItem
@@ -3641,32 +3641,36 @@
 ();%0A            
+   
+ 
 ((NavListAdapter
@@ -3708,32 +3708,46 @@
 taSetChanged();%0A
+            %7D%0A
         %7D%0A    %7D%0A

Fix crashes with NavDrawer when running in Strict Mode