package k57ca.pmp.askeverywhere;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
 
@SuppressLint("NewApi")
public class MainActivity extends ListActivity {
 
    private ProgressDialog pDialog;
 
    // URL to get contacts JSON
    private static String url = "http://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow&filter=withBody";
 
    // JSON Node names
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_ADDRESS = "address";
    private static final String TAG_GENDER = "gender";
    private static final String TAG_PHONE = "phone";
    private static final String TAG_PHONE_MOBILE = "mobile";
    private static final String TAG_PHONE_HOME = "home";
    private static final String TAG_PHONE_OFFICE = "office";
    
    private static final String TAG_ITEMS = "items";
    private static final String TAG_TITLE = "title";
    private static final String TAG_LINK = "link";
    private static final String TAG_BODY = "body";
 
    // contacts JSONArray
    JSONArray items = null;
 
    // Hashmap for ListView
    public ArrayList<HashMap<String, String>> itemList;
    
    public static String[] titles = new String[30];
    public static String[] bodys = new String[30];
    int index = 0;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        itemList = new ArrayList<HashMap<String, String>>();
 
        ListView lv = getListView();
 
        // Listview on item click listener
        lv.setOnItemClickListener(new OnItemClickListener() {
 
            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                    int position, long id) {
                // getting values from selected ListItem
                String title = ((TextView) view.findViewById(R.id.title))
                        .getText().toString();
                String description = ((TextView) view.findViewById(R.id.body))
                        .getText().toString();
  	    	  view.animate().setDuration(1000).alpha(0).withEndAction(new Runnable() {
	              @Override
	              public void run() {
//	                list.remove(item);
//	                adapter.notifyDataSetChanged();
	                view.setAlpha(1);
	              }
	          });
                Log.d(title, description);
            }
        });
 
        // Calling async task to get json
        new GetContacts().execute();
    }
 
    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetContacts extends AsyncTask<Void, Void, Void> {
 
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
 
        }
 
        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();
 
            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
 
            Log.d("Response: ", "> " + jsonStr);
 
            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                     
                    // Getting JSON Array node
                    items = jsonObj.getJSONArray(TAG_ITEMS);
 
                    // looping through All Contacts
                    for (int i = 0; i < items.length(); i++) {
                        JSONObject q = items.getJSONObject(i);
                         
                        String title = q.getString(TAG_TITLE);
                        String link = q.getString(TAG_LINK);
                        String body = q.getString(TAG_BODY);
                        titles[index] = title;
                        bodys[index] = body;
                        
                        Log.d(title, body);
                        
                        index++;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }
 
            return null;
        }
 
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, itemList,
                    R.layout.question, new String[] { TAG_TITLE, TAG_LINK },
                    			new int[] { R.id.title, R.id.body});
 
            setListAdapter(adapter);
        }
 
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
		int base = Menu.FIRST;
		MenuItem search = menu.add(base, 1, 1, "Newsfeed");
//		MenuItem addQuestion = menu.add(base, 2, 2, "Add question");
//		MenuItem help = menu.add(base, 3, 3, "Help");
        return true;
    }

    @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.
        switch (item.getItemId()) {
        case 1:
        	IntentsUtils.newsfeed(this);
        	break;
        case 2:
        	break;
        case 3:
        	break;
        default:
        	return super.onOptionsItemSelected(item);
        }
        return true;
    }
    
}
@@ -2179,1034 +2179,8 @@
 ;%0A %0A
-        itemList = new ArrayList%3CHashMap%3CString, String%3E%3E();%0A %0A        ListView lv = getListView();%0A %0A        // Listview on item click listener%0A        lv.setOnItemClickListener(new OnItemClickListener() %7B%0A %0A            @Override%0A            public void onItemClick(AdapterView%3C?%3E parent, final View view,%0A                    int position, long id) %7B%0A                // getting values from selected ListItem%0A                String title = ((TextView) view.findViewById(R.id.title))%0A                        .getText().toString();%0A                String description = ((TextView) view.findViewById(R.id.body))%0A                        .getText().toString();%0A  %09    %09  view.animate().setDuration(1000).alpha(0).withEndAction(new Runnable() %7B%0A%09              @Override%0A%09              public void run() %7B%0A//%09                list.remove(item);%0A//%09                adapter.notifyDataSetChanged();%0A%09                view.setAlpha(1);%0A%09              %7D%0A%09          %7D);%0A                Log.d(title, description);%0A            %7D%0A        %7D);%0A %0A
     
@@ -2232,23 +2232,24 @@
  new Get
-Contact
+Question
 s().exec
@@ -2358,23 +2358,24 @@
 lass Get
-Contact
+Question
 s extend
@@ -4564,286 +4564,8 @@
  */%0A
-            ListAdapter adapter = new SimpleAdapter(%0A                    MainActivity.this, itemList,%0A                    R.layout.question, new String%5B%5D %7B TAG_TITLE, TAG_LINK %7D,%0A                    %09%09%09new int%5B%5D %7B R.id.title, R.id.body%7D);%0A %0A            setListAdapter(adapter);%0A
     

delete view in MainActivity