package org.yacos.web.PCmember.controller;



import java.util.*;
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.springframework.web.servlet.view.RedirectView;
import org.yacos.core.article.*;
import org.yacos.core.conferences.Conference;
import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.evaluation.Criterion;
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.User;

//import org.yacos.web.author.form.*;
import org.yacos.web.PCmember.form.FormEvaluation;

import org.yacos.web.PCmember.form.SimpleCriterion;
import org.yacos.web.system.session.SessionService;

public class EvaluationController  extends SimpleFormController {

	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

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

	public IEvaluationManager getEvaluationManager() {
		return evaluationManager;
	}

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

	public Log getLogger() {
		return logger;
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		
		Conference conf=SessionService.getInstance().getCurrentConference();
		List<Criterion> criterionList =	evaluationManager.getCriterions(conf.getId());
		
		String id=request.getParameter("id");
		Article art=articleManager.getArticle(Integer.parseInt(id));
		String title=art.getTitle();
		
		FormEvaluation fe=new FormEvaluation();
		
		List<SimpleCriterion> liste=new ArrayList<SimpleCriterion>();
		for (Criterion c : criterionList)
			
      {  
		
   SimpleCriterion sc=new SimpleCriterion(c.getName(),c.getMin_rating(),c.getMax_rating());
			
      liste.add(sc);
		
      }
		fe.setSimpleRatings(liste);
		fe.setCommentAuthor("no comment by author");
		fe.setCommentPCMember("no comment by pc member");
		fe.setTitle(title);
		 return fe;
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
	throws Exception {
		String id=request.getParameter("id");
        logger.info("my article's id is "+id);
        
        
		FormEvaluation  fe=(FormEvaluation)command;

		List<SimpleCriterion> liste=fe.getSimpleRatings();
		String commentAuthor=fe.getCommentAuthor();
		String commentPCMember=fe.getCommentPCMember();
		List<Rating> listeRating=new ArrayList<Rating>(); 
		
		for(SimpleCriterion sc : liste){
			
			Rating rt=new Rating();
			Criterion cn=new Criterion();
			cn.setName(sc.getName());
			rt.setCriterion(cn);
			rt.setValue(sc.getValue());
			listeRating.add(rt);
		}
        
		
		
        
     //   Article article=articleManager.getArticle(new Integer(1));
		Article article =articleManager.getArticle(Integer.parseInt(id));
		
        System.out.println("le title est: "+article.getTitle());
        User current = SessionService.getInstance().getCurrentUser();
		if(current == null){
			current = userManager.addUser("toto",User.hashPassword("toto"), "Toto", "Toto", "Toto corp.", "toto@totocorp.com");
		}
		
		int idd=Integer.parseInt(id);
				
	   Report rpt= evaluationManager.addReport(idd, commentPCMember, commentAuthor, listeRating, current.getLogin());
	    request.getSession().setAttribute("report", rpt);
	  //persister ratings dans la base apres avoir le id report.
		System.out.println("lengthe is :"+listeRating.size());
		for (Rating rt : listeRating)
			
		{
			 System.out.println("value: "+rt.getValue());
			 System.out.println("cretertion: "+rt.getCriterion());
			evaluationManager.addRating(rt.getValue(), rt.getCriterion(), rpt);
		}
	    
	    
		return new ModelAndView(new RedirectView("createReport.htm"));
	}

	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 IConferenceManager getConferenceManager() {
		return conferenceManager;
	}

	public void setConferenceManager(IConferenceManager conferenceManager) {
		this.conferenceManager = conferenceManager;
	}
}