package org.yacos.web.PCmember.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.article.Article.State;
import org.yacos.core.evaluation.IEvaluationManager;
import org.yacos.core.evaluation.Rating;
import org.yacos.core.evaluation.Report;
import org.yacos.core.users.IUserManager;
import org.yacos.core.users.Role.RoleType;
import org.yacos.web.system.controller.NoConferenceSelectedException;
import org.yacos.web.system.session.SessionService;

public class ListReportController extends SimpleFormController {

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

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


	public IArticleManager getArticleManager() {
		return articleManager;
	}

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

	public IEvaluationManager getEvaluationManager() {
		return evaluationManager;
	}

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

	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 listReport view");

		List<Report> listReport=new ArrayList<Report>();
		String reportId = request.getParameter("id");

		try {
			Integer id = new Integer(reportId);
			Article a = articleManager.getArticle(id);
			if (a == null)
				throw new ModelAndViewDefiningException(new ModelAndView("404error"));

			Map <String, Object> model = new HashMap<String, Object>();

			if (userManager.hasRoleForConference(SessionService.getInstance().getCurrentUserLogin(), RoleType.CHAIRMAN, SessionService.getInstance().getCurrentConferenceId())) {
				listReport = evaluationManager.getReportsForArticle(id); 

				if (listReport.isEmpty()) {
					model.put("whereIcome", request.getHeader("REFERER"));
					model.put("listReport", null);
					return new ModelAndView("listReport", model);
				}
				else { 
					model.put("listReport", listReport);

					List<List<Rating>> container = new ArrayList<List<Rating>>();

					List<Double> listMoyenne = new ArrayList<Double>();

					
					List<Rating> ratings = new ArrayList<Rating>();
					for (int i = 0; i <= listReport.size() - 1; i++) {
						ratings = evaluationManager.getRatingForReport(listReport.get(i).getId());
						container.add(ratings);
					}
					
					//init liste moyenne
					
					for (int j=0; j < ratings.size(); j++)
						listMoyenne.add(new Double(0));
					
					double sum = 0;
					int i = 0;
					for (List<Rating> listRt : container) {
						for (Rating r : listRt) {
							sum = listMoyenne.get(i);
							sum += r.getValue();
							listMoyenne.set(i, sum);
							i++;
						}
						i=0;
					}
					
					for (int j=0; j < ratings.size(); j++) {	
						listMoyenne.set(j, listMoyenne.get(j) / new Double(listReport.size()));
					}
					
					model.put("container", container);
					model.put("listMoyenne", listMoyenne);
				}

			}

			else if	(userManager.hasRoleForConference(SessionService.getInstance().getCurrentUserLogin(), RoleType.PCMEMBER, SessionService.getInstance().getCurrentConferenceId())) {
				if (a.getState() == State.ACCEPTED ||a.getState() == State.REJECTED) {
					listReport = evaluationManager.getReportsForArticle(id);

					if (listReport.isEmpty()) {
						model.put("whereIcome", request.getHeader("REFERER"));
						model.put("listReport", null);
						return new ModelAndView("listReport", model);
					}
					else {
						model.put("listReport", listReport);

						List<List<Rating>> container = new ArrayList<List<Rating>>();

						for (int i = 0; i <= listReport.size() - 1; i++) {
							List<Rating> ratings = evaluationManager.getRatingForReport(listReport.get(i).getId());
							container.add(ratings);
						}
						model.put("container", container);
					}
				}

				else {	
					Report rpt = evaluationManager.getReportforArticleAndReferee(id, SessionService.getInstance().getCurrentUserLogin());

					if (rpt == null) {
						model.put("whereIcome", request.getHeader("REFERER"));
						model.put("rpt", null);
						return new ModelAndView("listReport", model);
					}
					else {
						model.put("rpt", rpt); 
						List<Rating> ratings = evaluationManager.getRatingForReport(rpt.getId());
						model.put("rtgs",ratings);
					}
				}

			}

			model.put("whereIcome", request.getHeader("REFERER"));



			listReport = evaluationManager.getReportsForArticle(id);










			return new ModelAndView("listReport", model);
		}

		catch (NumberFormatException e) {
			throw new ModelAndViewDefiningException(new ModelAndView("404error"));
		}
	}
}