package org.yacos.web.PCmember.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
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.ModelAndViewDefiningException;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.yacos.core.article.Article;
import org.yacos.core.article.IArticleManager;
import org.yacos.core.conferences.Conference;
import org.yacos.core.exceptions.ConferenceDoesntExistException;
import org.yacos.core.forum.IMessageManager;
import org.yacos.core.users.IUserManager;
import org.yacos.core.users.Role.RoleType;
import org.yacos.web.PCmember.form.ArticleForumBean;
import org.yacos.web.system.controller.NoConferenceSelectedException;
import org.yacos.web.system.session.SessionService;

public class ForumController extends SimpleFormController {

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

	private IArticleManager articleManager;
	private IMessageManager messageManager;
	private IUserManager userManager;
	
	public IMessageManager getMessageManager() {
		return messageManager;
	}

	public void setMessageManager(IMessageManager messageManager) {
		this.messageManager = messageManager;
	}

	public IArticleManager getArticleManager() {
		return articleManager;
	}

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

	public IUserManager getUserManager() {
		return userManager;
	}

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

	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException, NoConferenceSelectedException, ConferenceDoesntExistException {

		logger.info("Returning the forum view");

		Conference conf = SessionService.getInstance().getCurrentConference();
		List<Article> listArticle = new ArrayList<Article>();

		String person = request.getParameter("role");
		
		if(person == null || person.equals("")){
			throw new ModelAndViewDefiningException(new ModelAndView("404error"));
		}
		
		List<ArticleForumBean> listArticleBean = new ArrayList<ArticleForumBean>();
		
		if (person.equals("pcmember")) {
			if (userManager.hasRoleForConference(SessionService.getInstance().getCurrentUserLogin(), RoleType.PCMEMBER, conf.getId())) {
				listArticle = articleManager.getArticlesOfMember(conf.getId(), SessionService.getInstance().getCurrentUserLogin());
				
				for (Article a : listArticle) {
					ArticleForumBean afb = new ArticleForumBean();
					afb.setA(a);
					afb.setNumberMessage(messageManager.getMessagesForArticle(a.getId()).size());
					listArticleBean.add(afb);
				}
			}
			else
				throw new ModelAndViewDefiningException(new ModelAndView("denied"));
		}
		else if (person.equals("chairman")) {
			if (userManager.hasRoleForConference(SessionService.getInstance().getCurrentUserLogin(), RoleType.CHAIRMAN, conf.getId())) {
				listArticle = articleManager.getArticles(conf.getId());
				
				for (Article a : listArticle) {
					ArticleForumBean afb = new ArticleForumBean();
					afb.setA(a);
					afb.setNumberMessage(messageManager.getMessagesForArticle(a.getId()).size());
					listArticleBean.add(afb);
				}
			}
			else
				throw new ModelAndViewDefiningException(new ModelAndView("denied"));
		}
		else
			throw new ModelAndViewDefiningException(new ModelAndView("denied"));


		return new ModelAndView("forum", "listArticle", listArticleBean);
	}
}
