package org.yacos.tests.core;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
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.conferences.Conference;
import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.exceptions.NoConferenceCreationTokenLeftException;
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;


public class ConferenceManagerTest {
	private static IConferenceManager cm;
	private static IUserManager um;
	private int id;
	private Conference conf;
	
	@BeforeClass
	public static void setUpManager() throws NamingException, PKAlreadyUsedException {
		Context context = new InitialContext();
		cm = (IConferenceManager) context.lookup("ConferenceManagerBean/remote");
		um = (IUserManager) context.lookup("UserManagerBean/remote");
	}
	
	@Before
	public void setUp() throws Exception {
		if(!um.exists("max")){
			um.addUser("max",User.hashPassword("pw"),"maxime","dagn","ipint", "e@e.fr");
		} else {
			um.getUser("max");
		}
		cm.addConferenceCreationToken("max");
		conf = cm.addConference("titre","max","desc conf","info en plus",new Date(),new Date(),new Date(),new Date(),new Date());
		id = conf.getId();
	}
	
	@Test
	public void addConferenceTest() throws NoConferenceCreationTokenLeftException{
		cm.addConferenceCreationToken("max");
		conf = cm.addConference("titi","max","desc conf","info en plus",new Date(),new Date(),new Date(),new Date(),new Date());
		assertEquals("titi", cm.getConference(conf.getId()).getTitle());
		cm.remove(conf.getId());
	}
		
	
	@Test
	public void getConferenceTest(){
		conf = cm.getConference(id);
		assertNotNull(conf);
	}
	
	@Test
	public void updateConferenceTest(){
		conf = cm.getConference(id);
		conf.setTitle("autre titre");
		cm.update(conf);
		conf = cm.getConference(id);
		assertEquals("autre titre", conf.getTitle());
	}
	
	public void removeConferenceTest() throws NoConferenceCreationTokenLeftException{
		conf = cm.getConference(id);
		cm.remove(conf.getId());
		cm.addConferenceCreationToken("max");
		cm.addConference("titre","max","desc conf","info en plus",new Date(),new Date(),new Date(),new Date(),new Date());
	}
	
	@Test
	public void getConferencesTest() throws NoConferenceCreationTokenLeftException{
		cm.addConferenceCreationToken("max");
		conf = cm.addConference("titre2","max","desc conf","info en plus",new Date(),new Date(),new Date(),new Date(),new Date());
		List<Conference> list = cm.getConferences();
		assertFalse("liste vide",list.isEmpty());
		cm.remove(conf.getId());
	}
	
	@Test
	public void addRoleForUserTest(){
		cm.addRole(Role.RoleType.AUTHOR, "max" , id);
	}
		
	@Test
	public void getRolesForConfTest(){
		List<Role> list = cm.getRoles("max",conf.getId());
		assertTrue("liste vide",list.isEmpty());
		cm.addRole(Role.RoleType.AUTHOR, "max" , id);
	}
	
	@Test
	public void getConferencesForUserTest() throws PKAlreadyUsedException, UserEMailAlreadyExistsException{
		User user = um.getUser("max");
		cm.addRole(Role.RoleType.AUTHOR, "max" , id);
		List<Conference> list = cm.getConferences(user);
		assertFalse(list.isEmpty());
		User user2 = um.addUser("max2",User.hashPassword("pw"),"maxime","dagn","ipint", "eMax2@ee.fr");
		list = cm.getConferences(user2);
		assertTrue(list.isEmpty());
		um.removeUser(user2.getLogin());
	}

	@Test
	public void getRolesForUserAndConfTest(){
		cm.addRole(Role.RoleType.AUTHOR, "max" , id);
		List<Role> list = cm.getRoles("max", conf.getId());
		assertFalse("liste vide",list.isEmpty());
		Role role = list.get(0);
		assertNotNull(role);	
	}
	
	@Test
	public void testConferenceExists(){
		assertTrue(cm.exists(id));
		cm.remove(id);
		assertFalse(cm.exists(id));
	}
	
	@After
	public void cleanup(){
		if(um.exists("max")){
			um.removeUser("max");
		}
		
		if(cm.exists(id)){
			cm.remove(id);
		}
	}
}
