package org.idlesoft.android.hubroid;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

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

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class WatchedRepositories extends ListActivity {
	private RepositoriesListAdapter m_adapter;
	public ProgressDialog m_progressDialog;
	public JSONObject m_jsonData;
	public int m_position;
	public String m_username;
	public SharedPreferences m_prefs;
	public Intent m_intent;

	public RepositoriesListAdapter initializeList() {
		RepositoriesListAdapter adapter = null;
		try {
			URL query = new URL("http://github.com/api/v2/json/repos/watched/"
								+ URLEncoder.encode(getSharedPreferences(Hubroid.PREFS_NAME, 0).getString("login", "")));
			m_jsonData = Hubroid.make_api_request(query);
			if (m_jsonData == null) {
				runOnUiThread(new Runnable() {
					public void run() {
						Toast.makeText(WatchedRepositories.this, "Error gathering repository data, please try again.", Toast.LENGTH_SHORT).show();
					}
				});
			} else {
				adapter = new RepositoriesListAdapter(getApplicationContext(), m_jsonData.getJSONArray("repositories"));
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return adapter;
	}
	
	private Runnable threadProc_initializeList = new Runnable() {
		public void run() {
			m_adapter = initializeList();

			runOnUiThread(new Runnable() {
				public void run() {
					if (m_adapter != null) {
						setListAdapter(m_adapter);
					}
					m_progressDialog.dismiss();
				}
			});
		}
	};

	private Runnable threadProc_itemClick = new Runnable() {
		public void run() {
			try {
	        	m_intent = new Intent(WatchedRepositories.this, RepositoryInfo.class);
	        	m_intent.putExtra("repo_name", m_jsonData.getJSONArray("repositories").getJSONObject(m_position).getString("name"));
	        	m_intent.putExtra("username", m_jsonData.getJSONArray("repositories").getJSONObject(m_position).getString("owner"));
			} catch (JSONException e) {
				e.printStackTrace();
			}

			runOnUiThread(new Runnable() {
				public void run() {
					WatchedRepositories.this.startActivity(m_intent);
				}
			});
		}
	};
	
	private OnItemClickListener m_MessageClickedHandler = new OnItemClickListener() {
		public void onItemClick(AdapterView parent, View v, int position, long id) {
			m_position = position;
			Thread thread = new Thread(null, threadProc_itemClick);
			thread.start();
		}
	};

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.watched_repositories);

        m_prefs = getSharedPreferences(Hubroid.PREFS_NAME, 0);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
	        if(extras.containsKey("username")) {
	        	m_username = icicle.getString("username");
	        } else {
	        	m_username = m_prefs.getString("login", "");
	        }
        } else {
        	m_username = m_prefs.getString("login", "");
        }

        TextView title = (TextView)findViewById(R.id.tv_watched_repositories_title);
        title.setText(m_username + "'s fan club memberships:");
    }

    @Override
    public void onStart() {
    	super.onStart();

    	m_progressDialog = ProgressDialog.show(WatchedRepositories.this, "Please wait...", "Searching Repositories...", true);
		Thread thread = new Thread(null, threadProc_initializeList);
		thread.start();

        ListView list = (ListView)findViewById(android.R.id.list);
        list.setOnItemClickListener(m_MessageClickedHandler);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    	if (m_jsonData != null) {
    		savedInstanceState.putString("json", m_jsonData.toString());
    	}
    	super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
    	super.onRestoreInstanceState(savedInstanceState);
    	boolean keepGoing = true;
    	try {
    		if (savedInstanceState.containsKey("json")) {
    			m_jsonData = new JSONObject(savedInstanceState.getString("json"));
    		} else {
    			keepGoing = false;
    		}
		} catch (JSONException e) {
			keepGoing = false;
		}
		if (keepGoing == true) {
			try {
				m_adapter = new RepositoriesListAdapter(getApplicationContext(), m_jsonData.getJSONArray("repositories"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} else {
			m_adapter = null;
		}
    }

    @Override
    public void onResume() {
    	super.onResume();
    	if (m_adapter != null) {
    		setListAdapter(m_adapter);
    	}
    }
}
@@ -3726,14 +3726,12 @@
 %22, %22
-Search
+Load
 ing 

Tweak dialog message