package com.newsblur.fragment;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.newsblur.R;
import com.newsblur.domain.UserDetails;
import com.newsblur.domain.ActivityDetails;
import com.newsblur.network.APIManager;
import com.newsblur.network.domain.ActivitiesResponse;
import com.newsblur.view.ActivitiesAdapter;

public class ProfileActivityFragment extends Fragment {

	private ListView activityList;
	private ActivitiesAdapter adapter;
	private APIManager apiManager;
	private UserDetails user;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		apiManager = new APIManager(getActivity());
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View v = inflater.inflate(R.layout.fragment_profileactivity, null);
		activityList = (ListView) v.findViewById(R.id.profile_details_activitylist);
		if (adapter != null) {
			displayActivities();
		}
		activityList.setOnScrollListener(new EndlessScrollListener());
		return v;
	}
	
	public void setUser(Context context, UserDetails user) {
		this.user = user;
		Log.d("mark", "set user to = " + user.username);
		adapter = new ActivitiesAdapter(context, user);
		displayActivities();
	}
	
	private void displayActivities() {
		activityList.setAdapter(adapter);
		loadPage(1);
	}

	private void loadPage(final int pageNumber) {
		// TODO progress indicator
		// TODO pass limit?
		new AsyncTask<Void, Void, ActivityDetails[]>() {

			@Override
			protected ActivityDetails[] doInBackground(Void... voids) {
				Log.d("mark", "user.id = " + user.id);
				Log.d("mark", "user.userId = " + user.userId);
				Log.d("mark", "pageNumber = " + pageNumber);
				// For the logged in user user.userId is null.
				// From the user intent user.userId is the number while user.id is prefixed with social:
				String id = user.userId;
				if (id == null) {
					id = user.id;
				}
				ActivitiesResponse activitiesResponse = apiManager.getActivities(id, pageNumber);
				if (activitiesResponse != null) {
					return activitiesResponse.activities;
				} else {
					return new ActivityDetails[0];
				}
			}

			@Override
			protected void onPostExecute(ActivityDetails[] result) {
				for (ActivityDetails activity : result) {
					adapter.add(activity);
				}
				adapter.notifyDataSetChanged();
			}
		}.execute();
	}

	/**
	 * Detects when user is close to the end of the current page and starts loading the next page
	 * so the user will not have to wait (that much) for the next entries.
	 *
	 * @author Ognyan Bankov
	 *
	 * https://github.com/ogrebgr/android_volley_examples/blob/master/src/com/github/volley_examples/Act_NetworkListView.java
	 */
	public class EndlessScrollListener implements AbsListView.OnScrollListener {
		// how many entries earlier to start loading next page
		private int visibleThreshold = 5;
		private int currentPage = 1;
		private int previousTotal = 0;
		private boolean loading = true;

		public EndlessScrollListener() {
		}
		public EndlessScrollListener(int visibleThreshold) {
			this.visibleThreshold = visibleThreshold;
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
							 int visibleItemCount, int totalItemCount) {
			if (loading) {
				if (totalItemCount > previousTotal) {
					loading = false;
					previousTotal = totalItemCount;
					currentPage++;
				}
			}
			if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
				// I load the next page of gigs using a background task,
				// but you can call any function here.
				loadPage(currentPage);
				loading = true;
			}
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {

		}
	}
}

@@ -572,16 +572,59 @@
 Adapter;
+%0Aimport com.newsblur.view.ProgressThrobber;
 %0A%0Apublic
@@ -801,18 +801,70 @@
 s user;%0A
-%0A%09
+    private ProgressThrobber footerProgressView;%0A%0A    
 @Overrid
@@ -1272,16 +1272,620 @@
 ylist);%0A
+%0A        View footerView = inflater.inflate(R.layout.row_loading_throbber, null);%0A        footerProgressView = (ProgressThrobber) footerView.findViewById(R.id.itemlist_loading_throb);%0A        footerProgressView.setColors(getResources().getColor(R.color.refresh_1),%0A                                     getResources().getColor(R.color.refresh_2),%0A                                     getResources().getColor(R.color.refresh_3),%0A                getResources().getColor(R.color.refresh_4));%0A        activityList.addFooterView(footerView, null, false);%0A        activityList.setFooterDividersEnabled(false);%0A%0A
 %09%09if (ad
@@ -2360,59 +2360,8 @@
 ) %7B%0A
-%09%09// TODO progress indicator%0A%09%09// TODO pass limit?%0A
 %09%09ne
@@ -2408,19 +2408,173 @@
 %5D%3E() %7B%0A%0A
-%09%09%09
+            @Override%0A            protected void onPreExecute() %7B%0A                footerProgressView.setVisibility(View.VISIBLE);%0A            %7D%0A%0A            
 @Overrid
@@ -3418,16 +3418,77 @@
 nged();%0A
+                footerProgressView.setVisibility(View.GONE);%0A
 %09%09%09%7D%0A%09%09%7D

Add progress indicator when loading within the activity list.