package org.yacos.web.PCmember.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
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.evaluation.IEvaluationManager;
import org.yacos.core.evaluation.Report;
import org.yacos.core.users.IUserManager;
import org.yacos.core.users.User;
import org.yacos.core.users.Role.RoleType;
import org.yacos.web.PCmember.form.EvaluationArticleBean;
import org.yacos.web.system.controller.NoConferenceSelectedException;
import org.yacos.web.system.session.SessionService;

public class ListEvaluationController extends SimpleFormController {

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

	private IArticleManager articleManager;
	private IUserManager userManager;
	private IEvaluationManager evaluationManager;


	public IEvaluationManager getEvaluationManager() {
		return evaluationManager;
	}

	public void setEvaluationManager(IEvaluationManager evaluationManager) {
		this.evaluationManager = evaluationManager;
	}

	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 {

		logger.info("Returning the article's list to evaluate view");

		Conference conf = SessionService.getInstance().getCurrentConference();
		if(conf.getDateEvaluation().before(new Date())){
			return new ModelAndView("main","end","evaluation");
		}		
		List<Article> listArticle = new ArrayList<Article>();
		List<EvaluationArticleBean> listArticleBean = new ArrayList<EvaluationArticleBean>();

		String person = request.getParameter("role");
		if (person == null || person.equals(""))
			throw new ModelAndViewDefiningException(new ModelAndView("404error"));

		// TODO : renvoyer un truc s'il n'a pas le role

		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) {
					EvaluationArticleBean eab = new EvaluationArticleBean();
					eab.setArticle(a);

					Report rpt = evaluationManager.getReportforArticleAndReferee(a.getId(), SessionService.getInstance().getCurrentUserLogin());
					if (rpt == null) {
						eab.setEvaluated(false);
					}
					else {
						eab.setEvaluated(true);
					}
					
					List<User> referee = userManager.getRefereesOfPcMemberForArticle(SessionService.getInstance().getCurrentUserLogin(), a.getId());
		        	if (referee != null)
		        		eab.setReferee(referee);
		        	else
		        		eab.setReferee(null);
					
					listArticleBean.add(eab);

				}
			}
		}
		else if (person.equals("referee")) {
			if (userManager.hasRoleForConference(SessionService.getInstance().getCurrentUserLogin(), RoleType.REFEREE, conf.getId())) {
				List<Article> listArticleTmp;
				listArticleTmp = articleManager.getArticlesOfReferee(SessionService.getInstance().getCurrentUserLogin());
				if (listArticleTmp != null) {
					for (Article a : listArticleTmp) {
						Conference confTmp = articleManager.getConferenceForArticle(a.getId());
						if (confTmp.getId().equals(conf.getId()))
							listArticle.add(a);
					}
					for (Article a: listArticle) {
						EvaluationArticleBean eab = new EvaluationArticleBean();
						eab.setArticle(a);

						Report rpt = evaluationManager.getReportforArticleAndReferee(a.getId(), SessionService.getInstance().getCurrentUserLogin());
						if (rpt == null) {
							eab.setEvaluated(false);
						}
						else {
							eab.setEvaluated(true);
						}
						listArticleBean.add(eab);

					}
				}
			}
		}
		else
			throw new ModelAndViewDefiningException(new ModelAndView("404error"));


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