package org.yacos.web.PCmember.controller;

import java.util.ArrayList;
import java.util.Collection;

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.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.yacos.core.article.Article;
import org.yacos.core.article.IArticleManager;
import org.yacos.core.users.Role;
import org.yacos.core.users.User;
import org.yacos.web.PCmember.form.FormDispatcher;

public class DispatchArticleController extends SimpleFormController{

	protected final Log logger = LogFactory.getLog(getClass());
	
	private IArticleManager articleManager; 
	public IArticleManager getArticleManager() {
		return articleManager;
	}
	public void setArticleManager(IArticleManager articleManager) {
		this.articleManager = articleManager;
	}

	
	
	protected Object formBackingObject(HttpServletRequest request) {
		logger.info("Returning dispatcher view");
		/*test temporaire !!!!
		Article a = new Article(1,"title1","topic","url","autor",new ArrayList(),1);
		Article a2 = new Article(2,"title2","topic","url","autor",new ArrayList(),1);
		Collection<Article> articles = new ArrayList<Article>();
		articles.add(a);
		articles.add(a2);*/
		User m = new User("max","max","maxime","dagnicourt","ipint","m@truc.fr");
		User m2 = new User("bob","bob","bob","dupont","lifl","b@bb.fr");
		Collection<User> members = new ArrayList<User>();
		members.add(m);
		members.add(m2);
		request.getSession().setAttribute("members",members);
		request.getSession().setAttribute("articles",articleManager.getArticles());
		logger.info(articleManager.getArticles());
		return new FormDispatcher();
	}
	
	protected ModelAndView onSubmit(HttpServletRequest request, 
			HttpServletResponse response,
			Object formDispatcher, BindException errors) throws Exception {
		
		FormDispatcher fd = (FormDispatcher)formDispatcher;
		Article article = articleManager.getArticle(fd.getIdArticle());  
		User m = new User("bob","bob","maurice","dupont","lifl","b@bb.fr");
		User m2 = new User("max","max","maxime","dagnicourt","ipint","m@truc.fr");
		//article.assignToPCMember(m);
		Collection<User> members = new ArrayList<User>();
		members.add(m);
		members.add(m2);
		
		request.getSession().setAttribute("members",members);		
		
		return new ModelAndView("dispatchArticle", "articles", articleManager.getArticles());
	}
	
	
}
