/**
 * License Agreement.
 *
 *  JBoss RichFaces - Ajax4jsf Component Library
 *
 * Copyright (C) 2007  Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */
package org.richfaces.realworld.service;

import java.util.List;

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

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

@Name("userAction")
@Stateless
@AutoCreate
public class UserAction implements IUserAction {

	@In(value="entityManager")
	EntityManager em;
	
	@In(create = true, required = true) @Out
	private User user;

	public User login(String username, String password) {
		return user = (User)em.createNamedQuery(Constants.USER_LOGIN_QUERY)
		.setParameter(Constants.USERNAME_PARAMETER, username)
		.setParameter(Constants.PASSWORD_PARAMETER, password)
		.getSingleResult();
	}

	public void register(User user) {
		em.persist(user);
		em.flush();
	}
	
	public void updateUser(User user) {
		em.flush();
		this.user = user;
	}
	
	@SuppressWarnings("unchecked")
	public List<Album> getRootAlbums(User user) {
		List<Album> albums = em.createNamedQuery(Constants.USER_ROOT_ALBUMS_QUERY)
		.setParameter(Constants.LOGIN_PARAMETER, user.getLogin())
		.getResultList();
		return albums;
	}
	
	public void resetUser(User user) {
		em.refresh(user);
		em.flush();
		this.user = user;
	}
	
	public boolean isUserExist(String login) {
		return em.createNamedQuery(Constants.USER_EXIST_QUERY)
		.setParameter(Constants.LOGIN_PARAMETER, login)
		.getResultList().size() != 0;
	}

	@SuppressWarnings("unchecked")
	public List<String> getUsers(String suggest) {
		List<String> users = em.createNamedQuery(Constants.USER_AVAILABLE_USERS_QUERY)
		.setParameter(Constants.LOGIN_PARAMETER, suggest + Constants.PERCENT)
		.setMaxResults(10)
		.getResultList();
		return users;
	}
	
	public void removeFromFriends(User owner, User removed) {
		owner.removeFriend(removed);
		removed.removeFriend(owner);
		em.flush();
	}

	public boolean friendExist(User user, User friend) {
		Long result = (Long)em.createNamedQuery(Constants.USER_FRIEND_EXIST_QUERY)
		.setParameter(Constants.LOGIN_PARAMETER, user.getLogin())
		.setParameter(Constants.FRIEND_PARAMETER, friend.getLogin())
		.getSingleResult();
		return result > 0;
	}

	public void removeFromFavoriteAlbums(User user, Album album) {
		user.removeFromFavoriteAlbums(album);
		em.flush();
	}

	public void addFavoriteAlbum(Album album) {
		user.addFavoriteAlbum(album);
		album.getSharedOwners().add(user);
		em.flush();
	}

	public void loginAnonymous() {
		user = new User();	
	}

	public void addFavoriteImage(Image image) {
		user.addFavoriteImage(image);
		image.getSharedOwners().add(user);
		em.flush();
	}

	public void removeFromFavoriteImages(User user, Image image) {
		user.removeFromFavoriteImages(image);
		em.flush();
	}
}