package org.richfaces.realworld.service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.richfaces.realworld.domain.Image;

@Name("searchService")
public class SearchService implements ISearchService, Serializable
 {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2750591521413940277L;
	//@PersistenceContext(unitName = "entityManager", type=PersistenceContextType.EXTENDED)
	@In(value="entityManager")
	EntityManager em;
	
	/* (non-Javadoc)
	 * @see org.richfaces.realworld.service.ISearchService#searchImages(java.lang.String)
	 */
	@Begin(join=true)
	public List<Image> searchImages(String searchPattern, String additionalParams, Map<String, Object> paramMap){
		String fullQuery = "select i from MetaTag t join t.parent i where t.tag like:metatag and i.album.shared=true" + additionalParams + " order by i.rank.total/i.rank.hits desc";
		Query prepared = em.createQuery(fullQuery).setParameter("metatag", searchPattern+"%");
		if(paramMap != null && paramMap.get(":spinnerValue") != null){
			prepared.setParameter("spinnerValue", paramMap.get(":spinnerValue"));
		}
		if(paramMap != null && paramMap.get(":choice") != null){
			prepared.setParameter("choice", paramMap.get(":choice"));
		}
		if(paramMap != null && paramMap.get(":date") != null){
			prepared.setParameter("date", paramMap.get(":date"));
		}
		List<Image> images = prepared.getResultList();
		for(Image image:images){
			image.getAlbum().getOwner().getLogin();
		}
		return images;
	}
	
	@Begin(join=true)
	public List<Image> popularImages(String additionalParams, Map<String, Object> paramMap){
		String fullQuery = "select i from Image i join i.rank r where i.album.shared=true" + additionalParams + " order by r.total/r.hits desc";
		Query prepared = em.createQuery(fullQuery);
		if(paramMap != null && paramMap.get(":spinnerValue") != null){
			prepared.setParameter("spinnerValue", paramMap.get(":spinnerValue"));
		}
		if(paramMap != null && paramMap.get(":choice") != null){
			prepared.setParameter("choice", paramMap.get(":choice"));
		}
		if(paramMap != null && paramMap.get(":date") != null){
			prepared.setParameter("date", paramMap.get(":date"));
		}
		List<Image> images = prepared.getResultList();
		for(Image image:images){
			image.getAlbum().getOwner().getLogin();
		}
		return images;
	}
	
	@Begin(join=true)
	public List<Image> worstImages(String additionalParams, Map<String, Object> paramMap){
		String fullQuery = "select i from Image i join i.rank r where i.album.shared=true" + additionalParams + " order by r.total/r.hits asc";
		Query prepared = em.createQuery(fullQuery);
		if(paramMap != null && paramMap.get(":spinnerValue") != null){
			prepared.setParameter("spinnerValue", paramMap.get(":spinnerValue"));
		}
		if(paramMap != null && paramMap.get(":choice") != null){
			prepared.setParameter("choice", paramMap.get(":choice"));
		}
		if(paramMap != null && paramMap.get(":date") != null){
			prepared.setParameter("date", paramMap.get(":date"));
		}
		List<Image> images = prepared.getResultList();
		for(Image image:images){
			image.getAlbum().getOwner().getLogin();
		}
		return images;
	}
	
	/* (non-Javadoc)
	 * @see org.richfaces.realworld.service.IAlbumAction#remove()
	 */
	@Remove
	public void remove(){
		
	}
	
	/* (non-Javadoc)
	 * @see org.richfaces.realworld.service.IAlbumAction#destroy()
	 */
	@Destroy
	public void destroy(){
		
	}
}
