package org.richfaces.realworld.service;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Query;

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.richfaces.realworld.domain.Image;
import org.richfaces.realworld.domain.Shelf;
import org.richfaces.realworld.domain.User;

@Name("searchAction")
@Stateless
@AutoCreate
@SuppressWarnings("unchecked")
//TODO add JavaDocs
public class SearchAction implements ISearchAction {

	@In(value="entityManager")
	EntityManager em;
	
	@In @Out
	private User user;
	
	public List<Image> searchByAlbum(String searchQuery, boolean searchInMyAlbums, boolean searchInShared) {
		StringBuilder b = new StringBuilder("from Album a where lower(a.name) like :name or lower(a.description) like :name");
		if (searchInMyAlbums) {
			b.append(" and a.owner.login=:login");
		}
		if(searchInShared){
			b.append(" and a.shelf.shared=:shared");
		}
		Query query = em.createQuery(b.toString());
		
		query.setParameter("name", "%" + searchQuery.toLowerCase() + "%");
		if(searchInShared){
			query.setParameter("shared", searchInShared);
		}
		if (searchInMyAlbums) {
			query.setParameter("login", user.getLogin());
		}
		return query.getResultList();
	}

	public List<Image> searchByImage(String searchQuery, boolean searchInMyAlbums, boolean searchInShared) {
		StringBuilder b = new StringBuilder("from Image i where lower(i.name) like :name or lower(i.description) like :name or lower(i.cameraModel) like :name");
		if (searchInMyAlbums) {
			b.append(" and i.album.owner.login=:login");
		}
		if(searchInShared){
			b.append(" and i.album.shelf.shared=:shared");
		}
		Query query = em.createQuery(b.toString());
		
		query.setParameter("name", "%" + searchQuery.toLowerCase() + "%");
		if(searchInShared){
			query.setParameter("shared", searchInShared);
		}
		if (searchInMyAlbums) {
			query.setParameter("login", user.getLogin());
		}
		return query.getResultList();
	}

	public List<Image> searchByUsers(String searchQuery, boolean searchInMyAlbums, boolean searchInShared) {
		StringBuilder b = new StringBuilder("select  u from User u where lower(u.login) like :name or lower(u.firstName) like :name or lower(u.secondName) like :name");
		Query query = em.createQuery(b.toString());
		
		query.setParameter("name", "%" + searchQuery.toLowerCase() + "%");
		
		return query.getResultList();
	}

	public List<Image> searchByTags(String searchQuery, boolean searchInMyAlbums, boolean searchInShared) {
		StringBuilder b = new StringBuilder("from MetaTag t where lower(t.tag) like :name");
		Query query = em.createQuery(b.toString());
		
		query.setParameter("name", "%" + searchQuery.toLowerCase() + "%");
		return query.getResultList();
	}

	public List<Shelf> searchByShelfs(String searchQuery, boolean searchInMyAlbums,
			boolean searchInShared) {
		StringBuilder b = new StringBuilder("from Shelf sh where lower(sh.name) like :name or lower(sh.description) like :name ");
		if (searchInMyAlbums) {
			b.append(" and sh.owner.login=:login");
		}
		if (searchInShared) {
			b.append(" and sh.shared=true");
		}
		Query query = em.createQuery(b.toString());
		if (searchInMyAlbums) {
			query.setParameter("login", user.getLogin());
		}
		query.setParameter("name", "%" + searchQuery.toLowerCase() + "%");
		return query.getResultList();
	}

}
