package org.richfaces.realworld.search;

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

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Events;
import org.richfaces.realworld.manager.NavigationEnum;
import org.richfaces.realworld.service.Constants;
import org.richfaces.realworld.service.ISearchAction;

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

	private static final long serialVersionUID = -304368268896942902L;
	
	@In ISearchAction searchAction;

	ISearchOption selectedOption;

	List<ISearchOption> options;
	String selectedTab;
	
	String searchQuery;
	
	List<String> keywords = new ArrayList<String>();
	
	boolean seachInMyAlbums;
	
	boolean searchInShared = true;
	
	public ImageSearchHelper() {
		options = new ArrayList<ISearchOption>();
		options.add(new SearchOptionByShelf());
		options.add(new SearchOptionByAlbum());
		options.add(new SearchOptionByImage());
		options.add(new SearchOptionByUser());
		options.add(new SearchOptionByTag());
	}
	
	public String getCriteriaString(){
		StringBuilder s = new StringBuilder();
		for(ISearchOption option:options) {
			if(option.getSelected()){
				s.append(option.getName() + ",");
			}
		}
		return s.toString();
	}
	
	public void search() {
		keywords = new ArrayList<String>();
		Events.instance().raiseEvent(Constants.UPDATE_MAIN_AREA_EVENT, NavigationEnum.SEARCH);
		keywords = parse(searchQuery);
		Iterator<ISearchOption> it = options.iterator();
		while (it.hasNext()) {
			ISearchOption option = it.next();
			if (option.getSelected()) {
				option.search(searchAction, keywords.get(0).trim(), seachInMyAlbums, searchInShared);
			}else {
				option.setSearchResult(null);
			}
		}
	}
	
	public void search(String keyword) {
		Iterator<ISearchOption> it = options.iterator();
		while (it.hasNext()) {
			ISearchOption option = it.next();
			if (option.getSelected()) {
				option.search(searchAction, keyword.trim(), seachInMyAlbums, searchInShared);
			}else {
				option.setSearchResult(null);
			}
		}
	}
	
	private List<String> parse(String searchQuery2) {
		return Arrays.asList(searchQuery2.split(","));
	}

	boolean isOptionSelected() {
		return selectedOption != null;
	}
	
	public void processSelection() {
		Iterator<ISearchOption> it = options.iterator();
		while (it.hasNext()) {
			ISearchOption option = it.next();
			if (option.getSelected()) {
				selectedOption = option;
				break;
			}
		}
	}
	
	@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;
	}

	public String getSearchQuery() {
		return searchQuery;
	}

	public void setSearchQuery(String searchQuery) {
		this.searchQuery = searchQuery;
	}

	public boolean isSeachInMyAlbums() {
		return seachInMyAlbums;
	}

	public void setSeachInMyAlbums(boolean seachInMyAlbums) {
		this.seachInMyAlbums = seachInMyAlbums;
	}

	public boolean isSearchInShared() {
		return searchInShared;
	}

	public void setSearchInShared(boolean searchInShared) {
		this.searchInShared = searchInShared;
	}

	public List<String> getKeywords() {
		return keywords;
	}

	public void setKeywords(List<String> keywords) {
		this.keywords = keywords;
	}
	
}
