package org.yacos.tests.core;

import static org.junit.Assert.*;

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

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.yacos.core.article.Article;
import org.yacos.core.article.IArticleManager;
import org.yacos.core.conferences.Conference;
import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.exceptions.PKAlreadyUsedException;
import org.yacos.core.exceptions.UserEMailAlreadyExistsException;
import org.yacos.core.users.IUserManager;
import org.yacos.core.users.Role;
import org.yacos.core.users.User;
import org.yacos.core.users.Role.RoleType;


public class UserManagerTest {
	private static IUserManager um;
	private static IConferenceManager cm;
	private static IArticleManager am;
	private static int conf_id;
	
	@BeforeClass
	public static void setUpManager() throws NamingException, PKAlreadyUsedException  {
		Context context = new InitialContext();

		um = (IUserManager) context.lookup("UserManagerBean/remote");
		am = (IArticleManager) context.lookup("ArticleManagerBean/remote");
		cm = (IConferenceManager) context.lookup("ConferenceManagerBean/remote");
		if (cm.exists(1)){
			conf_id = 1;
		}else{
			Conference conf = cm.addConference("titi","desc conf","info en plus",new Date(),new Date(),new Date(),new Date(),new Date());
			conf_id = conf.getId();
		}
	}
	
	@Before
	public void setup()throws PKAlreadyUsedException, UserEMailAlreadyExistsException{
		if(!um.exists("truc")){
			um.addUser("truc",User.hashPassword("pw"),"maxime","dagn","ipint", "e@e.fr");
		}
	}
	
	@Test
	public void addUserTest() throws PKAlreadyUsedException, UserEMailAlreadyExistsException{
		User user = um.addUser("maxmax",User.hashPassword("pw"),"maxime","dagn","ipint", "uie@e.fr");
		assertEquals(User.hashPassword("pw"), user.getPassword());
		um.removeUser("maxmax");
	}
	
	@Test
	public void getUserTest(){
		User user = um.getUser("truc");
		assertNotNull(user);
		assertEquals("truc", user.getLogin());
	}
	
	@Test
	public void getUserByRoleType(){
		Role role = cm.addRole(RoleType.PCMEMBER, "truc", conf_id);
		List<User> list = um.getUsers(conf_id,RoleType.PCMEMBER);
		assertFalse(list.isEmpty());
		//on recupere le dernier user de la liste
		User user = list.get(list.size()-1);
		assertEquals("truc",user.getLogin());
	}
	
	
	
	@Test
	public void getPCMemberForArticleTest(){
		cm.addRole(RoleType.PCMEMBER, "truc", conf_id);
		Article article = am.addArticle("title","topic","url_article","admin",
				new ArrayList<String>(),Article.State.SUMMARY,conf_id);
		am.assignArticleToPCMember(article.getId(), "truc");
		List <User> list = um.getPCMemberForArticle(article.getId());
		assertFalse("liste vide",list.isEmpty());
		assertEquals("truc",list.get(list.size()-1).getLogin());
		am.unassignArticleToPCMember(article.getId(), "truc");
		am.removeArticle(article.getId());
	}
			
	@Test
	public void getRefereesForArticleTest(){
		cm.addRole(RoleType.PCMEMBER, "truc", conf_id);
		Article article = am.addArticle("title","topic","url_article","truc",
				new ArrayList<String>(),Article.State.SUMMARY,conf_id);
		am.delegateArticleToReferee(article.getId(), "truc", "max");
		List <User> list = um.getRefereesOFPCMemberForArticle("max",article.getId());
		assertFalse("liste vide",list.isEmpty());
		assertEquals("truc",list.get(list.size()-1).getLogin());
	}
	
	
	@After
	public void ControleUser() throws PKAlreadyUsedException{
		if(um.exists("truc")){
			um.removeUser("truc");
		}
	}
	

}
