package ca.teamTen.recitopia;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * Displays a list of recipes. Selecting a recipe
 * will open it in RecipeViewActivity.
 * 
 * The list is refreshed every time the activity is resumed.
 *
 */
public class RecipeListActivity extends ListActivity {

	Recipe recipes[];
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_recipelist);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		// Refresh list contents, as they may have changed.
		// TODO: be more sophisticated
		if(getIntent().getStringExtra("type")=="My Recipes"){
			recipes = ApplicationManager.getInstance(getApplication())
					.getUserRecipeBook().query("");
		}
		else if (getIntent().getStringExtra("type")=="My Favorite"){
			recipes = ApplicationManager.getInstance(getApplication())
					.getFavoriteRecipesBook().query("");
		}

		
		ListAdapter adapter = new ArrayAdapter<String>(
				this,
				android.R.layout.simple_list_item_activated_1,
				android.R.id.text1,
				makeListItems(recipes));
		
		setListAdapter(adapter);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		Recipe recipe = this.recipes[position];
		
		Intent viewIntent = new Intent(this, RecipeViewActivity.class);
		viewIntent.putExtra("RECIPE", recipe);
		
		startActivity(viewIntent);
	}
	
	private String[] makeListItems(Recipe[] recipes) {
		String[] result = new String[recipes.length];
		
		for (int i = 0; i < recipes.length; i++) {
			result[i] = recipes[i].getRecipeName() + "\n" + recipes[i].getAuthor();
		}
		return result;
	}
}

@@ -827,26 +827,32 @@
 xtra(%22type%22)
-==
+.equals(
 %22My Recipes%22
@@ -852,16 +852,17 @@
 ecipes%22)
+)
 %7B%0A%09%09%09rec
@@ -950,24 +950,51 @@
 .query(%22%22);%0A
+%09%09%09setTitle(%22My Recipes%22);%0A
 %09%09%7D%0A%09%09else i
@@ -1034,10 +1034,16 @@
 pe%22)
-==
+.equals(
 %22My 
@@ -1052,16 +1052,17 @@
 vorite%22)
+)
 %7B%0A%09%09%09rec
@@ -1155,24 +1155,60 @@
 .query(%22%22);%0A
+%09%09%09setTitle(%22My Favorite Recipes%22);%0A
 %09%09%7D%0A%0A%09%09%0A%09%09Li

add favorite recipes function