package k57ca.pmp.askeverywhere;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("NewApi")
public class Newsfeed extends Activity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

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

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
        
		final ListView listview = (ListView) findViewById(R.id.list_view);
	    String[] values = new String[] { "Stack Overflow", "Quora", "Stack Overflow", 
	    				"ABC", "XYZ", "Stack Overflow", "Stack Overflow", "Question1", 
	    				"Stack Overflow", "Stack Overflow", "Stack Overflow",
	    				"Question2", "Android/Eclipse: how can I add an image in the res/drawable folder?",
	    				"Can The Android drawable directory contain subdirectories?",
	    				"In the Android SDK documentation, all of the examples used with the " +
	    				"@drawable/my_image xml syntax directly address images that are stored in" +
	    				" the res/drawable directory in my project.",
	    				"Stack Overflow" };

	    final ArrayList<String> list = new ArrayList<String>();
	    for (int i = 0; i < values.length; ++i) {
	    	list.add(values[i]);
	    }
	    final MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
	    listview.setAdapter(adapter);

	    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

	      @Override
	      public void onItemClick(AdapterView<?> parent, final View view,
	          int position, long id) {
	    	  final String item = (String) parent.getItemAtPosition(position);
	    	  view.animate().setDuration(1000).alpha(0).withEndAction(new Runnable() {
	              @Override
	              public void run() {
	                list.remove(item);
	                adapter.notifyDataSetChanged();
	                view.setAlpha(1);
	              }
	          });
	      }

	    });
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit();
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_section1);
                break;
            case 2:
                mTitle = getString(R.string.title_section2);
                break;
            case 3:
                mTitle = getString(R.string.title_section3);
                break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.newsfeed_temp, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_newsfeed, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((Newsfeed) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }

}

@@ -4290,439 +4290,201 @@
     
-    if (!mNavigationDrawerFragment.isDrawerOpen()) %7B%0A            // Only show items in the action bar relevant to this screen%0A            // if the drawer is not showing. Otherwise, let the drawer%0A            // decide what to show in the action bar.%0A            getMenuInflater().inflate(R.menu.newsfeed_temp, menu);%0A            restoreActionBar();%0A            return true;%0A        %7D%0A        return super.onCreateOptionsMenu(menu)
+%09super.onCreateOptionsMenu(menu);%0A%09%09int base = Menu.FIRST;%0A%09%09MenuItem search = menu.add(base, 1, 1, %22Search%22);%0A%09%09MenuItem addQuestion = menu.add(base, 2, 2, %22Add question%22);%0A        return true
 ;%0A  
@@ -4777,17 +4777,16 @@
     
-int id = 
+switch (
 item
@@ -4801,75 +4801,74 @@
 Id()
-;%0A        if (id == R.id.action_settings) %7B%0A            return true
+) %7B%0A        case 1:%0A        %09break;%0A        case 2:%0A        %09break
 ;%0A  
@@ -4869,25 +4869,32 @@
 ak;%0A        
-%7D
+default:
 %0A        ret
@@ -4886,24 +4886,25 @@
 lt:%0A        
+%09
 return super
@@ -4933,16 +4933,47 @@
 (item);%0A
+        %7D%0A        return true;%0A
     %7D%0A%0A 

add option menu on newsfeed screen