package org.yacos.web.PCmember.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.yacos.core.article.Article;
import org.yacos.core.article.IArticleManager;
import org.yacos.core.article.Article.State;
import org.yacos.core.conferences.Conference;
import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.evaluation.IEvaluationManager;
import org.yacos.core.evaluation.Report;
import org.yacos.core.users.IUserManager;
import org.yacos.core.users.Speciality;
import org.yacos.core.users.User;
import org.yacos.core.users.Role.RoleType;
import org.yacos.web.system.controller.MailSenderService;
import org.yacos.web.system.session.SessionService;
import org.yacos.web.system.session.YACOSUtils;

public class DelegateArticleController implements Controller{

	protected final Log logger = LogFactory.getLog(getClass());

	private IArticleManager articleManager; 
	public IArticleManager getArticleManager() {
		return articleManager;
	}
	public void setArticleManager(IArticleManager articleManager) {
		this.articleManager = articleManager;
	}

	private IUserManager userManager;
	public IUserManager getUserManager() {
		return userManager;
	}
	public void setUserManager(IUserManager userManager) {
		this.userManager = userManager;
	}

	private IConferenceManager conferenceManager; 
	public IConferenceManager getConferenceManager() {
		return conferenceManager;
	}
	public void setConferenceManager(IConferenceManager conferenceManager) {
		this.conferenceManager = conferenceManager;
	}

	private IEvaluationManager evaluationManager;
	public IEvaluationManager getEvaluationManager() {
		return evaluationManager;
	}
	public void setEvaluationManager(IEvaluationManager evaluationManager) {
		this.evaluationManager = evaluationManager;
	}

	Conference conf;
	String memberId;

	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {

		conf = SessionService.getInstance().getCurrentConference();
		memberId = SessionService.getInstance().getCurrentUserLogin();
		String login = SessionService.getInstance().getCurrentUserLogin();
		List<Article> listArticlesTmp = articleManager.getArticlesOfMember(conf.getId(), login);
		List<Article> listArticles = new ArrayList<Article>();

		for (Article a : listArticlesTmp) {
			Report rpt = evaluationManager.getReportforArticleAndReferee(a.getId(), SessionService.getInstance().getCurrentUserLogin());
			if (rpt == null) {
				if (a.getState() != State.ACCEPTED && a.getState() != State.REJECTED)
					listArticles.add(a);
			}		
		}


		logger.info("returning delegateArticle with "+ listArticles.size() +" article");

		return new ModelAndView("delegateArticle","articles", listArticles);
	}

	public List<RefereeBean> getRefereesWithTheArticle(int articleId ){

		RefereeBean member;
		//Conference conf = SessionService.getInstance().getCurrentConference();

		//List de RefereeBean qui sera retourn�
		List<RefereeBean> articleMembers = new ArrayList<RefereeBean>();

		//list of the member who have this article
		List<User> articleMembersTmp = userManager.getRefereesOfPcMemberForArticle(memberId, articleId);

		for (User user : articleMembersTmp) {
			member = new RefereeBean();
			member.setFirstName(user.getFirstName());
			member.setLastName(user.getLastName());
			member.setLogin(user.getLogin());


			//recuperation des specialit�es
			List<String> spectialities = new ArrayList<String>();
			List<Speciality> listSpe = userManager.getSpecialitiesFromUser(member.getLogin());
			for (Speciality speciality : listSpe) {
				spectialities.add(speciality.getName());
			}
			member.setSpecialities(spectialities);

			//add the member to the return list
			articleMembers.add(member);
		}	

		return articleMembers;
	}

	public List<RefereeBean> getRefereesWithoutTheArticle(int articleId ){

		RefereeBean member;
		//Conference conf = SessionService.getInstance().getCurrentConference();

		//List de RefereeBean qui sera retourn�
		List<RefereeBean> members = new ArrayList<RefereeBean>();

		//list of all pcmembers of the conference 
		List<User> membersTmp = userManager.getUsers();

		//list of the member who have this article
		List<User> refereesTmp = userManager.getRefereesOfPcMemberForArticle(memberId, articleId);

		for (User user : membersTmp) {
			member = new RefereeBean();
			member.setFirstName(user.getFirstName());
			member.setLastName(user.getLastName());
			member.setLogin(user.getLogin());


			//recuperation des specialit�es
			List<String> spectialities = new ArrayList<String>();
			List<Speciality> listSpe = userManager.getSpecialitiesFromUser(member.getLogin());
			for (Speciality speciality : listSpe) {
				spectialities.add(speciality.getName());
			}
			member.setSpecialities(spectialities);

			if (!refereesTmp.contains(user) 
					&& !userManager.hasRoleForConference(user.getLogin(), RoleType.CHAIRMAN, conf.getId())
					&& !userManager.hasRoleForConference(user.getLogin(), RoleType.PCMEMBER, conf.getId())
			){
				members.add(member);
			}

		}
		logger.debug("return"+ members.size() +"members");
		return members;
	}


	public void addReferees(List<String> refereesId,int articleId){	
		for (String  refereeId : refereesId) {
			articleManager.delegateArticleToReferee(articleId, refereeId,memberId);
			if(!userManager.hasRoleForConference(refereeId, RoleType.REFEREE, conf.getId())){
				conferenceManager.addRole(RoleType.REFEREE, refereeId, conf.getId());
				// Send an email to the referee
				String mailSubject = "YACOS : you asked to review an article for a conference";
				String mailBody = "A member of the new conference "+SessionService.getInstance().getCurrentConference().getTitle();
				mailBody += " has added you to the Program Committee\n";
				mailBody += "You will be able to view, evaluate or delegate the evaluation of the articles that will be submitted\n";
				mailBody += YACOSUtils.fullURL("userBoard.htm");
				User user = userManager.getUser(refereeId);
				MailSenderService.getInstance().sendEMail(user.getEmail(), mailSubject, mailBody);
			}
		}
		logger.info("add referee to article");
	}

	public void removeReferees(List<String> refereesId,int articleId){

		for (String refereeId : refereesId) {	
			articleManager.undelegateArticleToReferee(articleId, refereeId, memberId);	
		}

		logger.info("remove members to article");		
	}
}
