package dbFiller;

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

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

import org.yacos.core.article.Article;
import org.yacos.core.article.IArticleManager;
import org.yacos.core.article.Preference.PreferenceType;
import org.yacos.core.conferences.Conference;
import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.evaluation.Criterion;
import org.yacos.core.evaluation.IEvaluationManager;
import org.yacos.core.exceptions.ConferenceDoesntExistException;
import org.yacos.core.exceptions.PKAlreadyUsedException;
import org.yacos.core.exceptions.UserEMailAlreadyExistsException;
import org.yacos.core.users.IUserManager;
import org.yacos.core.users.User;
import org.yacos.core.users.Role.RoleType;

/*
 * fill the database for developpement and test
 */

public class Filler {

	
	public static void main(String[] args) throws NamingException, PKAlreadyUsedException, ConferenceDoesntExistException {
		
		System.out.println("--> DEBUT REMPLISSAGE DATABASE");
		
		IArticleManager am;
		IConferenceManager cm;
		IUserManager um;
		IEvaluationManager em;
		//initialisation du context
		Context context;
		
			context = new InitialContext();
			am = (IArticleManager) context.lookup("ArticleManagerBean/remote");
			um = (IUserManager) context.lookup("UserManagerBean/remote");
			cm = (IConferenceManager) context.lookup("ConferenceManagerBean/remote");
			em = (IEvaluationManager) context.lookup("EvaluationManagerBean/remote");
			
			// Supprime tout avant de repeupler
//			for (Conference conf : cm.getConferences()) {
//				for (Article art : am.getArticles(conf.getId())) {
//					am.removeArticle(art.getId());
//				}
//				cm.remove(conf.getId());
//			}
		//	for (User util : um.getUsers()) {
				//um.removeUser(util.getLogin());
			//}
			
			
			//ajout de 2 conferences
			Conference conf = cm.addConference("titre conf 1","desc conf","info en plus",
					new Date(),new Date(),new Date(),new Date(),new Date());
			cm.addConference("titre conf 2","desc conf","info en plus",
					new Date(),new Date(),new Date(),new Date(),new Date());
			
			//ajout de 2 users
			User author;
			try {
				author = um.addUser("max",User.hashPassword("pw"),"maxime","dagn","ipint", "max@e.fr");
				User author2 = um.addUser("toto",User.hashPassword("pw"),"toto","titu","ipint", "toto@e.fr");
				User membre1 = um.addUser("login_membre1",User.hashPassword("pw"),"Jean","Bon","ipint", "meme1@e.fr");
				User membre2 = um.addUser("login_membre2",User.hashPassword("pw"),"Vincent","Tim","ipint", "meme2@e.fr");
				User membre3 = um.addUser("login_membre3",User.hashPassword("pw"),"Camille","Hon","ipint", "meme3@e.fr");
				User membre = um.addUser("login_membre",User.hashPassword("pw"),"nom_membre","meme","ipint", "meme4@e.fr");
				User admin = um.addUser("admin",User.hashPassword("admin"),"maxime","dagnicourt","ipint", "admin@yacos.org");

				//ajout des roles pr les users
				cm.addRole(RoleType.AUTHOR, author.getLogin(), conf.getId());
				cm.addRole(RoleType.PCMEMBER, membre1.getLogin(), conf.getId());
				cm.addRole(RoleType.PCMEMBER, membre2.getLogin(), conf.getId());
				cm.addRole(RoleType.PCMEMBER, membre3.getLogin(), conf.getId());
				cm.addRole(RoleType.AUTHOR, admin.getLogin(), conf.getId());
				cm.addRole(RoleType.CHAIRMAN, admin.getLogin(), conf.getId());
				cm.addRole(RoleType.PCMEMBER, admin.getLogin(), conf.getId());
				cm.addRole(RoleType.REFEREE, admin.getLogin(), conf.getId());
				
				//ajout d'un article pr le user "max"
				Article article = am.addArticle("title article max","topic","url_article",
						author.getLogin(),	new ArrayList<String>(), Article.State.SUMMARY,	conf.getId());
				am.addArticle("title article toto","topic","url_article",
						author2.getLogin(),	new ArrayList<String>(), Article.State.SUMMARY,	conf.getId());
				
				am.assignArticleToPCMember(article.getId(), "login_membre1");
				am.addPreference(article.getId(), "login_membre1", PreferenceType.LIKE);
				
			} catch (UserEMailAlreadyExistsException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			// ajout de critere
			
			Criterion c;
			
			c = em.addCriterion("testCriterion", 1, 5, 1);
			c = em.addCriterion("testCriterion2", 2, 5, 1);
			c = em.addCriterion("testCriterion3", 1, 5, 2);
			c = em.addCriterion("testCriterion4", 2, 5, 2);
			
		
			System.out.println("--> FIN OK");
		
	}
	
	
}
