package org.richfaces.realworld.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

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

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Destroy;
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.realworld.domain.Album;
import org.richfaces.realworld.domain.Comment;
import org.richfaces.realworld.domain.FriendshipRequest;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.domain.User;

@Name("userAction")
public class UserAction implements IUserAction {
	
	//@PersistenceContext(unitName = "entityManager", type=PersistenceContextType.EXTENDED)
	@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.createQuery("from User u where u.login = :username and u.password = :password")
		.setParameter("username", username)
		.setParameter("password", password)
		.getSingleResult();
	}
	
	@Remove
	public void remove(){
		
	}
	
	@Destroy
	public void destroy(){
		
	}

	public void register(User user) {
		em.persist(user);
	}
	
	public void updateUser(User user) {
		//em.merge(user);
		em.flush();
		this.user = user;
	}
	
	public void resetUser(User user) {
		em.refresh(user);
		em.flush();
		this.user = user;
	}

	public boolean isUserExist(String login) {
		try{
		User tempuser = (User)em.createQuery("from User u where u.login = :login")
		.setParameter("login", login)
		.getSingleResult();
		}catch(Exception e){
			return false;
		}
		return true;
	}

	public List<String> getUsers(String suggest) {
		List<String> users = em.createQuery("select u.login from User u where u.login like :login").setParameter("login", suggest + "%").setMaxResults(10).getResultList();
		return users;
	}
	
	public long countNotReadedMessages(User user){
		Long result = (Long)em.createQuery("SELECT count(u) FROM User u JOIN u.messages mg WHERE u.login=:login and mg.readed = false").setParameter("login", user.getLogin()).getSingleResult();
		return result;
	}

	public void removeFromFriends(User owner, User removed) {
		owner.removeFriend(removed);
		removed.removeFriend(owner);
	}

	public boolean friendExist(User user, User friend) {
		Long result = (Long)em.createQuery("SELECT count(u) FROM User u JOIN u.friendshipRequests fr WHERE u.login=:login and fr.friend.login=:friend").setParameter("login", user.getLogin()).setParameter("friend", friend.getLogin()).getSingleResult();
		return result > 0;
	}

	public void addFriend(User user, User friend, FriendshipRequest request) {
		friend.removeFriendshipRequest(request);
		user.addFriend(friend);
		friend.addFriend(user);
	}

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

	public void addSharedAlbum(Album album) {
		user.addSharedAlbum(album);
		
	}

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