package org.yacos.web.chairman.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.Preference;
import org.yacos.core.conferences.Conference;
import org.yacos.core.conferences.IConferenceManager;
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.session.SessionService;

public class DispatchArticleController 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;
		
	}
	
	// FIXME : c'est interdit de faire ca ;)
	Conference conf;
	
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
				
		conf = SessionService.getInstance().getCurrentConference();
		List<Article> listArticles = articleManager.getArticles(conf.getId());
		logger.info("returning dispatchArticleList with "+ listArticles.size() +" article");
		return new ModelAndView("dispatchArticle","articles",listArticles);
	}

public List<PCMemberBean> getMembersWithTheArticle(int articleId ){
		
		PCMemberBean member;
		//Conference conf = SessionService.getInstance().getCurrentConference();
								
		//List de pcmemberbean qui sera retourné
		List<PCMemberBean> articleMembers = new ArrayList<PCMemberBean>();
		
		//list of the member who have this article
		List<User> articleMembersTmp = userManager.getPCMemberForArticle(articleId);
				
		for (User user : articleMembersTmp) {
			member = new PCMemberBean();
			member.setFirstName(user.getFirstName());
			member.setLastName(user.getLastName());
			member.setLogin(user.getLogin());
			
			//recuperation des preferences
			Preference pref = articleManager.getPreferenceForUserAndArticle(articleId, user.getLogin());
			if (pref != null){
				String prefType = pref.getPreferenceType().toString();
				member.setPreference(prefType);
			}else {
				member.setPreference("INDIFFERENT");
			}
			
			//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);
			
			//recuperatiojn du nombre d'article
			int nb = 0;
			List<Article> articles = articleManager.getArticlesOfMember(conf.getId(), user.getLogin());
			if(articles != null){
				nb = articles.size();
			}
			member.setNbArticles(nb);	
			
			//add the member to the return list
			articleMembers.add(member);
		}	
							
		return articleMembers;
	}
	
	public List<PCMemberBean> getMembersWithoutTheArticle(int articleId ){
		
		PCMemberBean member;
		//Conference conf = SessionService.getInstance().getCurrentConference();
						
		//List de pcmemberbean qui sera retourné
		List<PCMemberBean> members = new ArrayList<PCMemberBean>();
				
		//list of the member who have this article
		List<User> articleMembersTmp = userManager.getPCMemberForArticle(articleId);
		
		//list of all pcmembers of the conference 
		List<User> membersTmp = userManager.getUsers(conf.getId(),RoleType.PCMEMBER);
		
		for (User user : membersTmp) {
			member = new PCMemberBean();
			member.setFirstName(user.getFirstName());
			member.setLastName(user.getLastName());
			member.setLogin(user.getLogin());
			
			//recuperation des preferences
			Preference pref = articleManager.getPreferenceForUserAndArticle(articleId, user.getLogin());
			if (pref != null){
				String prefType = pref.getPreferenceType().toString();
				member.setPreference(prefType);
			}else {
				member.setPreference("INDIFFERENT");
			}
			
			//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);
			
			//recuperatiojn du nombre d'article
			int nb = 0;
			List<Article> articles = articleManager.getArticlesOfMember(conf.getId(), user.getLogin());
			if(articles != null){
				nb = articles.size();
			}
			member.setNbArticles(nb);	
			
			//on place le memebre dans une des listes
			if (!articleMembersTmp.contains(user) && member.getPreference()!= "CONFLICT" ){
				members.add(member);
			}
		}
		logger.debug("return"+ members.size() +"members");
		return members;
	}
	
	
	public void addMembers(List<String> membersId,int articleId){	
		for (String memberId : membersId) {
			articleManager.assignArticleToPCMember(articleId, memberId);
		}
		logger.debug("add members to article");
	}

	public void removeMembers(List<String> membersId,int articleId){
		for (String memberId : membersId) {
			articleManager.unassignArticleToPCMember(articleId, memberId);
		}
		logger.debug("remove members to article");
	}
	
	
}
