package ly.priv.mobile.gui.socialnetworks;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.ls.LSInput;

import ly.priv.mobile.R;
import ly.priv.mobile.ShowContent;
import ly.priv.mobile.Utilities;
import ly.priv.mobile.Values;

import com.actionbarsherlock.app.SherlockFragment;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphObject;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.renderscript.Sampler.Value;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SListUserMessagesActivity extends SherlockFragment {
	private static final String TAG = "SListUserMessagesActivity";
	private ArrayList<SMessage> mListUserMess;
	private ListUserMessagesAdapter mListUserMessagesAdapter;
	private ListView mListViewUserMessages;
	private ProgressBar mProgressBar;
	private Session mSession;
	private String mDialogID;
	private String mFaceBookUserId;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_list, container, false);
		mListViewUserMessages = ((ListView) view.findViewById(R.id.lView));
		mProgressBar = (ProgressBar) view.findViewById(R.id.pbLoadingData);
		mDialogID =  getArguments().getString("DialogID");	
		mListUserMess =new ArrayList<SMessage>();
		Values values = new Values(getActivity());
		mFaceBookUserId = values.getFacebookID();
		mSession=Session.getActiveSession();
		Log.d(TAG, mDialogID);
		if(mSession!=null && mSession.isOpened()){
			getDialogFromFaceBook();
		}		
		mListViewUserMessages.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ArrayList<String> listOfUrls = Utilities.fetchPrivlyUrls(mListUserMess.get(position).getMessage());
				if(listOfUrls.size()>0){
				FragmentTransaction transaction = getActivity()
				 .getSupportFragmentManager().beginTransaction();
				 ShowContent showContent =new ShowContent();
				 Bundle bundle = new Bundle();
				 bundle.putStringArrayList("listOfLinks", listOfUrls);
				 showContent.setArguments(bundle);
				 transaction.replace(R.id.container,
						 showContent);
				 transaction.addToBackStack(null);
				 transaction.commit();
				}else{
					Toast.makeText(getActivity(), R.string.message_not_containe_privly_link,
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		return view;
	}
	
	/**
	 * this method is used by the facebook API
	 */
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (mSession != null) {
			mSession.onActivityResult(getActivity(), requestCode, resultCode,
					data);
		}


	}


	/**
	 * Get inbox from FaceBook
	 */
	private void getDialogFromFaceBook() {
		Log.d(TAG, "getDialogFromFaceBook");
		mProgressBar.setVisibility(View.VISIBLE);

		Bundle params = new Bundle();
		params.putString("fields",
				"comments.fields(from.fields(id,picture),message,created_time)");
		// params.putString("limit", "1");
		Request request = Request.newGraphPathRequest(mSession, mDialogID,
				new Request.Callback() {
					@Override
					public void onCompleted(Response response) {
						if (response.getError() != null) {
							Log.e(TAG, response.getError().getErrorMessage());
							mProgressBar.setVisibility(View.INVISIBLE);
							AlertDialog dialog = Utilities.showDialog(
									getActivity(),
									getString(R.string.error_inbox));
							dialog.show();
							return;
						}
						 GraphObject graphObject = response.getGraphObject();
						 try {							 
							JSONArray comments = graphObject.getInnerJSONObject().getJSONObject("comments").getJSONArray("data");
							Gson gson = new Gson();
							Type collectionType = new TypeToken<List<SMessage>>(){}.getType();
							mListUserMess=gson.fromJson(comments.toString(), collectionType);						
						} catch (JSONException e) {
								e.printStackTrace();
						}
						 
						if (mListUserMess != null) {
							mListUserMessagesAdapter = new ListUserMessagesAdapter(
									getActivity(), mListUserMess);
							mListViewUserMessages
									.setAdapter(mListUserMessagesAdapter);
							mListViewUserMessages.setSelection(mListUserMessagesAdapter.getCount() - 1);
						}
						mProgressBar.setVisibility(View.INVISIBLE);
					}
				});
		request.setParameters(params);
		request.executeAsync();
	}
}
@@ -124,126 +124,8 @@
 t;%0A%0A
-import org.json.JSONArray;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0Aimport org.w3c.dom.ls.LSInput;%0A%0A
 impo
@@ -255,278 +255,56 @@
 ort 
-com.actionbarsherlock.app.SherlockFragment;%0Aimport com.facebook.Request;%0Aimport com.facebook.Response;%0Aimport com.facebook.Session;%0Aimport com.facebook.SessionState;%0Aimport com.facebook.model.GraphObject;%0Aimport com.google.gson.*;%0Aimport com.google.gson.reflect.TypeToke
+org.json.JSONArray;%0Aimport org.json.JSONExceptio
 n;%0A%0A
@@ -396,51 +396,8 @@
 le;%0A
-import android.renderscript.Sampler.Value;%0A
 impo
@@ -472,38 +472,8 @@
 og;%0A
-import android.view.KeyEvent;%0A
 impo
@@ -592,24 +592,79 @@
 dapterView;%0A
+import android.widget.AdapterView.OnItemClickListener;%0A
 import andro
@@ -751,61 +751,256 @@
 st;%0A
+%0A
 import 
-android.widget.AdapterView.OnItemClickListener
+com.actionbarsherlock.app.SherlockFragment;%0Aimport com.facebook.Request;%0Aimport com.facebook.Response;%0Aimport com.facebook.Session;%0Aimport com.facebook.model.GraphObject;%0Aimport com.google.gson.Gson;%0Aimport com.google.gson.reflect.TypeToken
 ;%0A%0Ap
@@ -2053,16 +2053,17 @@
 ;%0A%09%09%7D%09%09%0A
+%0A
 %09%09mListV

Add Gson for SListUserMessagesActivity