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.conferences.Conference;
import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.evaluation.IEvaluationManager;
import org.yacos.core.exceptions.ConferenceDoesntExistException;
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.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, NoConferenceCreationTokenLeftException {

		System.out.println("--> DEBUT REMPLISSAGE DATABASE");

		IArticleManager am;
		IConferenceManager cm;
		IUserManager um;
		IEvaluationManager em;

		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");

		Conference conf = null;
		Conference conf2 = null;
		try {
			// Create different users
			User admin = um.addUser("admin",User.hashPassword("admin"),"Admin","admin","LIFL", "admin@yacos.org");
			User chairman = um.addUser("chairman",User.hashPassword("chairman"),"Chairman","chairman","LIFL", "chairman@yacos.org");
			User pcmember = um.addUser("pcmember",User.hashPassword("pcmember"),"PC","member","LIFL", "pcmember@yacos.org");
			User referee = um.addUser("referee",User.hashPassword("referee"),"Referee","referee","LIFL", "referee@yacos.org");
			User author = um.addUser("author",User.hashPassword("author"),"Author","author","LIFL", "author@yacos.org");
			
			// add two conference
			cm.addConferenceCreationToken("chairman");
			cm.addConferenceCreationToken("admin");
			conf = cm.addConference("Conf 1","chairman","Je suis une conf","J'aime le cafe",new Date(),new Date(),new Date(),new Date(),new Date(),
					0,
					true,
					false,
					true,
					false,
					"Other infos");
			conf2 = cm.addConference("Conf 2","admin","Je suis un schtroumpf","J'aime la salsepareille",new Date(),new Date(),new Date(),new Date(),new Date(),
					0,
					false,
					false,
					false,
					false,
					"Other infos");

			// Add role for users

			// admin
			cm.addRole(RoleType.CHAIRMAN, admin.getLogin(), conf.getId());
			cm.addRole(RoleType.PCMEMBER, admin.getLogin(), conf.getId());
			cm.addRole(RoleType.REFEREE, admin.getLogin(), conf.getId());
			cm.addRole(RoleType.AUTHOR, admin.getLogin(), conf.getId());
			// chairman
			cm.addRole(RoleType.CHAIRMAN, chairman.getLogin(), conf.getId());
			cm.addRole(RoleType.PCMEMBER, chairman.getLogin(), conf.getId());
			cm.addRole(RoleType.REFEREE, chairman.getLogin(), conf.getId());
			cm.addRole(RoleType.AUTHOR, chairman.getLogin(), conf.getId());
			// pcmember
			cm.addRole(RoleType.PCMEMBER, pcmember.getLogin(), conf.getId());
			cm.addRole(RoleType.REFEREE, pcmember.getLogin(), conf.getId());
			cm.addRole(RoleType.AUTHOR, pcmember.getLogin(), conf.getId());
			// referee
			cm.addRole(RoleType.REFEREE, referee.getLogin(), conf.getId());
			cm.addRole(RoleType.AUTHOR, referee.getLogin(), conf.getId());
			// author
			cm.addRole(RoleType.AUTHOR, author.getLogin(), conf.getId());

			
			
			// Add article for user
			// Of course, url isn't valid
			
			// chairman
			am.addArticle("Article chairman", "Les schtroumpfs sont fous !!!", "Abstract", "url_article",chairman.getLogin(),	new ArrayList<String>(), Article.State.SUBMITED, conf.getId());
			am.addArticle("Article chairman 2", "Les schtroumpfs sont fous 2 !!!", "Abstract", "url_article",chairman.getLogin(),	new ArrayList<String>(), Article.State.SUBMITED, conf.getId());
			am.addArticle("Article chairman 3", "Les schtroumpfs sont fous 3 !!!", "Abstract", "url_article",chairman.getLogin(),	new ArrayList<String>(), Article.State.SUBMITED, conf.getId());
			// pcmember
			am.addArticle("Article pcmember", "Sont fous ces gaulois :o)", "Abstract", "url_article",pcmember.getLogin(),	new ArrayList<String>(), Article.State.SUBMITED, conf.getId());
			am.addArticle("Article pcmember 2", "Sont fous ces gaulois 2 :o)", "Abstract", "url_article",pcmember.getLogin(),	new ArrayList<String>(), Article.State.SUBMITED, conf.getId());
			am.addArticle("Article pcmember 3", "Sont fous ces gaulois 3 :o)", "Abstract", "url_article",pcmember.getLogin(),	new ArrayList<String>(), Article.State.SUBMITED, conf.getId());
			// referee
			am.addArticle("Article referee", "Tappez des nains !!", "Abstract", "url_article",referee.getLogin(),	new ArrayList<String>(), Article.State.SUBMITED, conf.getId());
			am.addArticle("Article referee 2", "Tappez des nains 2 !!", "Abstract", "url_article",referee.getLogin(),	new ArrayList<String>(), Article.State.SUBMITED, conf.getId());
			am.addArticle("Article referee 3", "Tappez des nains 3 !!", "Abstract", "url_article",referee.getLogin(),	new ArrayList<String>(), Article.State.SUBMITED, conf.getId());
			// author
			am.addArticle("Article author", "Mangez un chien, chiez un chat !", "Abstract", "url_article",author.getLogin(),	new ArrayList<String>(), Article.State.SUBMITED, conf.getId());
			am.addArticle("Article author 2", "Mangez un chien, chiez un chat 2 !", "Abstract", "url_article",author.getLogin(),	new ArrayList<String>(), Article.State.SUBMITED, conf.getId());
			am.addArticle("Article author 3", "Mangez un chien, chiez un chat 3 !", "Abstract", "url_article",author.getLogin(),	new ArrayList<String>(), Article.State.SUBMITED, conf.getId());
			
			
		} catch (UserEMailAlreadyExistsException e) {
			e.printStackTrace();
		}

		// ajout de critere

		em.addCriterion("Les schtroumpfs sont-ils bons ?", 1, 5, conf.getId());
		em.addCriterion("Les gaulois sont-ils fous ?", 1, 4, conf.getId());
		em.addCriterion("Un nain est-il vraiment petit ?", 1, 5, conf2.getId());
		em.addCriterion("Un chien peut-il se transformer en chat ?", 1, 4, conf2.getId());

		System.out.println("--> FIN OK");

	}
}
