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.article.Preference.PreferenceType;
import org.yacos.core.conferences.Conference;
import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.users.IUserManager;
import org.yacos.core.users.User;
import org.yacos.core.users.Role.RoleType;
import org.yacos.web.system.session.SessionService;

public class ConflictListController 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;
	}
	
	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 confictArticleList with "+ listArticles.size() +" article");
		return new ModelAndView("conflictList","articles", listArticles);
	}

public List<PCMemberBean> getMembersWithConflict(int articleId ){
		
		PCMemberBean member;
		//Conference conf = SessionService.getInstance().getCurrentConference();
								
		//List de pcmemberbean qui sera retourné
		List<PCMemberBean> conflictMembers = new ArrayList<PCMemberBean>();
		
		//list of the member who have this article
		List<Preference> preferences = articleManager.getPreferencesForArticle(articleId);
		for (Preference pref : preferences) {
			if(pref.getPreferenceType() == PreferenceType.CONFLICT){
				User user = pref.getPcMember();
				member = new PCMemberBean();
				member.setFirstName(user.getFirstName());
				member.setLastName(user.getLastName());
				member.setLogin(user.getLogin());
				
				//add the member to the return list
				conflictMembers.add(member);
			}
			
		}						
		return conflictMembers;
	}
	
	public List<PCMemberBean> getMembersWithoutConflict(int articleId ){
		
		PCMemberBean member;
		//Conference conf = SessionService.getInstance().getCurrentConference();
						
		//List de pcmemberbean qui sera retourné
		List<PCMemberBean> members = new ArrayList<PCMemberBean>();
					
		//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){
				if(pref.getPreferenceType() != PreferenceType.CONFLICT){
					//add the member to the return list
					members.add(member);
				}
			}else{
				//add the member to the return list
				members.add(member);
			}
			
		}
		logger.debug("return"+ members.size() +"members");
		return members;
	}
	
	
	public void addMembers(List<String> membersId,int articleId){	
		for (String memberId : membersId) {
			articleManager.addPreference(articleId, memberId, PreferenceType.CONFLICT);
		}
		logger.info("add members to conflictlist");	
	}

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