package org.richfaces.realworld.search;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.model.SelectItem;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
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.richfaces.component.UIInputNumberSpinner;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.fileupload.FileManager;
import org.richfaces.realworld.service.ISearchService;

@Name("searchBean")
@Scope(ScopeType.CONVERSATION)
public class SearchBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5071655218132021316L;

	@In(required=false)
    private SearchBeanHelper helper;

	@In(create=true)
	private ISearchService searchService;
	
	private String searchPattern;
	
	private Long spinnerChoice;
	
	private Long dateChoice;
	
	private Long votesChoice;
	
	private Long numberOfVotes;
	
	private Date date;
	
	private List<Image> findedImages = new ArrayList<Image>();
	
	public List<SelectItem> getItems() {
	      List<SelectItem> list = new ArrayList<SelectItem>(4);
	      list.add(new SelectItem(Long.valueOf(0L), "No Matter"));
	      list.add(new SelectItem(Long.valueOf(1L), "More"));
	      list.add(new SelectItem(Long.valueOf(2L), "Less"));
	      list.add(new SelectItem(Long.valueOf(3L), "Equals"));
	      return list;
	   }
	
	private String getAstFromIndex(Long item){
		if(item == 1L){
			return "> ";
		}else if(item == 2L){
			return "< ";
		}else if(item == 3L){
			return "= ";
		}
		return null;
	}
	
	public List<Image> searchImages(){
		String additionalParams = populateAdditionalParams();
		Map<String, Object> paramMap = null;
		if(!additionalParams.equals("")){
			paramMap = populateMap(additionalParams);
		}
		findedImages = searchService.searchImages(this.searchPattern, additionalParams, paramMap);
		return findedImages;
	}
	
	public List<Image> popularImages(){
		String additionalParams = populateAdditionalParams();
		Map<String, Object> paramMap = null;
		if(!additionalParams.equals("")){
			paramMap = populateMap(additionalParams);
		}
		findedImages = searchService.popularImages(additionalParams, paramMap);
		return findedImages;
	}
	
	private Map<String, Object> populateMap(String additionalParams) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(additionalParams.lastIndexOf(":spinnerValue") != -1){
			map.put(":spinnerValue", Long.valueOf(helper.getInputSpinner().getValue().toString()));
		}
		if(additionalParams.lastIndexOf(":choice") != -1){
			map.put(":choice", numberOfVotes);
		}
		if(additionalParams.lastIndexOf(":date") != -1){
			map.put(":date", date);
		}
		return map;
	}

	private String populateAdditionalParams() {
		StringBuilder additionalParams = new StringBuilder("");
		if(spinnerChoice != null && spinnerChoice > 0L){
			additionalParams.append(" and i.rank.total/i.rank.hits");
			additionalParams.append(getAstFromIndex(spinnerChoice));
			additionalParams.append(":spinnerValue");
		}
		if(votesChoice != null && votesChoice > 0L){
			additionalParams.append(" and i.rank.hits");
			additionalParams.append(getAstFromIndex(votesChoice));
			additionalParams.append(":choice");
		}
		if(dateChoice != null && dateChoice > 0L){
			additionalParams.append(" and i.created");
			additionalParams.append(getAstFromIndex(dateChoice));
			additionalParams.append(":date");
		}
		return additionalParams.toString();
	}

	public List<Image> worstImages(){
		String additionalParams = populateAdditionalParams();
		Map<String, Object> paramMap = null;
		if(!additionalParams.equals("")){
			paramMap = populateMap(additionalParams);
		}
		findedImages = searchService.worstImages(additionalParams, paramMap);
		return findedImages;
	}

	public String getSearchPattern() {
		return searchPattern;
	}

	public void setSearchPattern(String searchPattern) {
		this.searchPattern = searchPattern;
	}

	public Long getSpinnerChoice() {
		return spinnerChoice;
	}

	public void setSpinnerChoice(Long spinnerChoice) {
		this.spinnerChoice = spinnerChoice;
	}

	public Long getDateChoice() {
		return dateChoice;
	}

	public void setDateChoice(Long dateChoice) {
		this.dateChoice = dateChoice;
	}

	public Long getVotesChoice() {
		return votesChoice;
	}

	public void setVotesChoice(Long votesChoice) {
		this.votesChoice = votesChoice;
	}

	public Long getNumberOfVotes() {
		return numberOfVotes;
	}

	public void setNumberOfVotes(Long numberOfVotes) {
		this.numberOfVotes = numberOfVotes;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public List<Image> getFindedImages() {
		return findedImages;
	}

	public void setFindedImages(List<Image> findedImages) {
		this.findedImages = findedImages;
	}
}
