/**
 * RecipeBook.java
 *
 *
 * Caitlin Ross
 * 100735219
 */

import java.util.*;

public class RecipeBook {
	
	private ArrayList<Recipe> recipeBook;
	private int length;
	
    public RecipeBook() {
    	recipeBook = new ArrayList<Recipe>();
    	length=0;
    }
    
    public ArrayList<Recipe> getRecipeArrayList(){
    	return recipeBook;
    }
    
    public void add(Recipe aRecipe){
    	recipeBook.add(aRecipe);
    	length++;
    }
    
    public Recipe[] getNames(){
    	Recipe recipeList[] = new Recipe[length];
    	for(int i=0; i<length; i++){
    		recipeList[i] = recipeBook.get(i);
    	}
    	for (int i = 0; i < length-1; i++) {
			for (int j = length-2; j>=i; j--) {
				if (recipeList[j].getName().compareToIgnoreCase(recipeList[j+1].getName())>0) {
					Recipe temp = recipeList[j];
					recipeList[j] = recipeList[j+1];
					recipeList[j+1] = temp;
				}
			}
		}
    	return recipeList;
    }
    
    public String[] getSpices(){
    	ArrayList<String> spiceList = new ArrayList<String>();
    	int spiceListLength = 0;
    	
    	for(int i=0; i<length; i++){
    		String spiceText = recipeBook.get(i).getSpice();
    		StringTokenizer tokenizer = new StringTokenizer(spiceText, "\n,.;:\t-()1234567890<>/-_=");
    		while(tokenizer.hasMoreTokens()){
    			spiceList.add(tokenizer.nextToken().trim());
    			spiceListLength++;
    		}
    	}
    	String spices[] = new String[spiceListLength];
    	
    	int cnt=0;
    	while(!spiceList.isEmpty()){
    		String nextSpice = spiceList.get(0);
    		int index = 0;
    		for(int i=0; i<spiceListLength; i++){
    			if(spiceList.get(i).compareToIgnoreCase(nextSpice)<0){
    				nextSpice = spiceList.get(i);
    				index = i;
    			}
    		}
    		if(cnt!=0){
    			boolean flag = true;
    			for(int i=0; i<cnt; i++){
    				if(spices[i].equalsIgnoreCase(nextSpice))
    					flag = false;
    			}
    			if(flag){
    				spices[cnt] = nextSpice;
    				cnt++;
    			}
    		}
    		else{
    			spices[cnt] = nextSpice;
    			cnt++;
    		}
    		spiceList.remove(index);
    		spiceListLength--;
    	}
    	return spices;
    }
    
    public Recipe[] recipesBySpice(String spice){
    	int cnt=0;
    	ArrayList<Recipe> recipes = new ArrayList<Recipe>();
    	for(int i=0; i<length; i++){
    		if(recipeBook.get(i).getSpice().contains(spice)){
    			recipes.add(recipeBook.get(i));
    			cnt++;
    		}
    	}
    	
    	Recipe[] recipeList = new Recipe[cnt];
    	
    	for(int i=0; i<cnt; i++){
    		recipeList[i] = recipes.get(i);
    	}
    	
    	return recipeList;
    }
    
    public Recipe[] findContributor(String contributor){
    	int cnt=0;
    	ArrayList<Recipe> recipes = new ArrayList<Recipe>();
    	for(int i=0; i<length; i++){
    		if(recipeBook.get(i).getContributor().equals(contributor)){
    			recipes.add(recipeBook.get(i));
    			cnt++;
    		}
    	}
    	
    	Recipe[] recipeList = new Recipe[cnt];
    	
    	for(int i=0; i<cnt; i++){
    		recipeList[i] = recipes.get(i);
    	}
    	
    	return recipeList;
    }
    
}