/* $Id: SearchQueryAction.java 626 2013-01-13 18:51:36Z shayashi $ */
package smart_gs.image_search;

import java.awt.Rectangle;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import smart_gs.image_search.logical.TextType;
import smart_gs.image_search.logical.GSQuery;
import smart_gs.logical.Spread;
import sml_editor.logical.LineDirection;


public class SearchQueryAction extends SearchAction{
	
	private GSQuery query;
	
	private ArrayList<ImageSearchFilter> imageSearchFilterList = new ArrayList<ImageSearchFilter>();

	public SearchQueryAction(GSQuery query,List<Spread> spreadsToSearch) {
		super(spreadsToSearch);
		LineDirection lineDirection = query.getLineDirection();
		
		Rectangle rect = query.getRectangle();
		//20080715 shimizu wrote
		if(lineDirection == LineDirection.HORIZONTAL){
			rect = new Rectangle(rect.x,rect.y+(rect.height/2),rect.width,1);
		}else if (lineDirection == LineDirection.VERTICAL) {
			//縦書きバージョン
			rect = new Rectangle(rect.x+(rect.width/2),rect.y,1,rect.height);
		}else {
			rect = new Rectangle(rect.x+(rect.width),rect.y,1,rect.height);
		}
		
		if(query.getSpread() == null){
			this.query = new GSQuery(query.getQueryImage(),query.getDSCFile(),rect,lineDirection);
			this.query.setURIs();
		}else{
			this.query = new GSQuery(query.getSpread(),query.getDSCFile(),rect);
			this.query.setURIs();
		}
	}
	
	public void execute(){
		GSSearchEngine searchEngine = GSSearchEngine.getInstance();
		searchEngine.setQuery(query);
		searchEngine.setSearchScope(spreadsToSearch);
		searchEngine.setImageSearchFilterList(imageSearchFilterList);
		searchEngine.execute();
		this.results = searchEngine.getResults();
	}
	
	public void addImageSearchFilter(ImageSearchFilter imageSearchFilter){
		imageSearchFilterList.add(imageSearchFilter);
	}
}
