package org.richfaces.realworld.search;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.event.ActionEvent;

import org.hibernate.validator.NotNull;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;

@Name("searchImageHelper")
@Scope(ScopeType.CONVERSATION)
public class ImageSearchHelper implements Serializable {

	private static final long serialVersionUID = -304368268896942902L;

	ISearchOption selectedOption;

	List<ISearchOption> options;
	
	public ImageSearchHelper() {
		options = new ArrayList<ISearchOption>();
		options.add(new SearchOptionByAlbum());
		options.add(new SearchOptionByImage());
	}
	
	boolean isOptionSelected() {
		return selectedOption != null;
	}
	
	public void processSelection(ActionEvent event) {
		Iterator<ISearchOption> it = options.iterator();
		while (it.hasNext()) {
			ISearchOption option = it.next();
			if (option.getSelected()) {
				selectedOption = option;
				break;
			}
		}
	}
	
	public void back(ActionEvent event) {
		Iterator<ISearchOption> it = options.iterator();
		while (it.hasNext()) {
			it.next().setSelected(false);
		}
		selectedOption = null;
	}
	
	@Out
	public String getTemplate() {
		return (isOptionSelected()) ? selectedOption.getTemplate() : "/includes/search/searchOptions.xhtml"; 
	}

	
	public ISearchOption getSelectedOption() {
		return selectedOption;
	}

	
	public void setSelectedOption(ISearchOption selectedOption) {
		this.selectedOption = selectedOption;
	}
	
	@Out
	public List<ISearchOption> getOptions() {
		return options;
	}

	public void setOptions(List<ISearchOption> options) {
		this.options = options;
	}
	
	

	
}
