package org.yacos.web.chairman.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.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;
	}
	
	
	public ModelAndView handleRequest(HttpServletRequest req,
			HttpServletResponse arg1) throws Exception {
		logger.info("Returning dispatcher view");
		
		Conference conf = SessionService.getInstance().getCurrentConference();
		
		//description of the article
		Article article = articleManager.getArticle(Integer.parseInt(req.getParameter("articleId")));
		
		//list of the member who have this article
		List<User> articleMembers = userManager.getPCMemberForArticle(article.getId());
		logger.debug("nombre de memebre prour l'article = "+articleMembers.size());
		
		//List members for webpage
		List<PCMemberBean> membersWeb = new ArrayList<PCMemberBean>();
		
		//list of member who have'nt this article
		List<User> members = userManager.getUsers(conf.getId(),RoleType.PCMEMBER);
		
		for (User user : members) {
			PCMemberBean member = new PCMemberBean();
			member.setFirstName(user.getFirstName());
			member.setLastName(user.getLastName());
			member.setLogin(user.getLogin());
			Preference pref = articleManager.getPreferenceForUserAndArticle(article.getId(), user.getLogin());
			if (pref != null){
				String prefType = pref.getPreferenceType().toString();
				member.setPreference(prefType);
			}else {
				member.setPreference(Preference.PreferenceType.INDIFFERENT.toString());
			}
			int nb = 0;
			List<Article> articles = articleManager.getArticlesOfMember(conf.getId(), user.getLogin());
			if(articles != null){
				nb = articles.size();
			}
			member.setNbArticles(nb);
			membersWeb.add(member);
		}	
		
		Map<String,Object> model = new HashMap<String,Object>();
		model.put("articleMembers",articleMembers);
		model.put("members", membersWeb);
		model.put("article", article);
				
		return new ModelAndView("dispatchArticle", model);
	}

	public List<User> addMember(String memberId,int articleId){
		
		articleManager.assignArticleToPCMember(articleId, memberId);
		return userManager.getPCMemberForArticle(articleId);
	}
	
	
	public String sayHello(){
		return "hello";
	}
	

	/*protected Object formBackingObject(HttpServletRequest request) throws ConferenceDoesntExistException {
		logger.info("Returning dispatcher view");

		FormDispatcher fd = new FormDispatcher();
		
		Conference conf = SessionService.getInstance().getCurrentConference();
		List<User> members = userManager.getUsers(conf.getId(),RoleType.PCMEMBER);
		request.getSession().setAttribute("members", members);
		List<Article> listArticles = articleManager.getArticles(conf.getId());
		request.getSession().setAttribute("articles", listArticles);
		
		HashMap <Integer,List<User>>articlesMembers = new HashMap<Integer,List<User>>();
		for (Article article : listArticles) {
			articlesMembers.put(article.getId(),userManager.getPCMemberForArticle(article.getId()));
		}
		request.getSession().setAttribute("articlesMembers", articlesMembers);
				
		return fd;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		logger.info("Dispatch: In the referenceData method, adding object in the model");
		
		Map model = new HashMap();
		Conference conf = SessionService.getInstance().getCurrentConference();
		Collection<User> members = userManager.getUsers(conf.getId(),RoleType.PCMEMBER);
		model.put("articles", articleManager.getArticles(conf.getId()));
		model.put("members", members);
		return model;
	}

	
	@SuppressWarnings("unchecked")
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
				
		logger.info("Dispatch: In the onSubmit method");
		
		Map model = new HashMap();
		Conference conf = SessionService.getInstance().getCurrentConference();
		Collection<User> members = userManager.getUsers(conf.getId(),RoleType.PCMEMBER);
		model.put("articles", articleManager.getArticles(conf.getId()));
		model.put("members", members);
		return new ModelAndView("dispatchArticle", model);
	}
*/
}
