package net.woodyfolsom.cs6601.p2;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.woodyfolsom.cs6601.p2.Ingredient.TYPE;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("ingredients")
public class Ingredients {
	@XStreamImplicit
	private List<Ingredient> ingredients;
	@XStreamImplicit
	private List<IngredientDivision> ingDivs;
	
	public boolean contains(Ingredient.TYPE ingredientType) {
		for (Ingredient ingredient : ingredients) {
			if (ingredient.isType(ingredientType)) {
				return true;
			}
		}
		for (IngredientDivision ingDiv : ingDivs) {
			if (ingDiv.contains(ingredientType)) {
				return true;
			}
		}
		return false;
	}
	
	public List<Ingredient> getIngredients() {
		return ingredients;
	}
	
	public Set<TYPE> getTypes() {
		Set<TYPE> types = new HashSet<TYPE>();
		for (Ingredient ingredient : ingredients) {
			types.addAll(ingredient.getTypes());
		}
		return types;
	}
	
	public void setIngredients(List<Ingredient> ingredients) {
		this.ingredients = ingredients;
	}
	
	public List<IngredientDivision> getIngDivs() {
		return ingDivs;
	}
	
	private Object readResolve() {
		if (ingDivs == null) {
			ingDivs = new ArrayList<IngredientDivision>();
		}
		if (ingredients == null) {
			ingredients = new ArrayList<Ingredient>();
		}
		return this;
	}
	
	public void setIngDivs(List<IngredientDivision> ingDivs) {
		this.ingDivs = ingDivs;
	}
}
