package org.yacos.web.system.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.mvc.SimpleFormController;
import org.yacos.core.article.Article;
import org.yacos.core.article.IArticleManager;
import org.yacos.core.conferences.Conference;
import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.evaluation.IEvaluationManager;
import org.yacos.core.users.IUserManager;
import org.yacos.core.users.Role;
import org.yacos.core.users.Role.RoleType;
import org.yacos.web.system.controller.NoConferenceSelectedException;
import org.yacos.web.system.session.SessionService;

public class UserBoardController extends SimpleFormController {

    protected final Log logger = LogFactory.getLog(getClass());
    
    private IArticleManager articleManager;
	private IUserManager userManager;
	private IConferenceManager conferenceManager;
	private IEvaluationManager 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 IConferenceManager getConferenceManager() {
		return conferenceManager;
	}

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

	public IEvaluationManager getEvaluationManager() {
		return evaluationManager;
	}

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

	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, NoConferenceSelectedException {
		
        logger.info("Returning UserBoard view");
        
        Map<String, Object> model = new HashMap<String, Object>();
        
        logger.info("Get the list of roles of an user");
        List<Role> listRole = userManager.getRolesForUser(SessionService.getInstance().getCurrentUserLogin());
        
        Integer numberChairman = 0;
        Integer numberPCMember = 0;
        Integer numberReferee = 0;
        Integer numberAuthor = 0;
        
        for (Role r : listRole) {
        	if (r.getType() == RoleType.CHAIRMAN)
        		numberChairman++;
        	else if (r.getType() == RoleType.PCMEMBER)
        		numberPCMember++;
        	else if (r.getType() == RoleType.REFEREE)
        		numberReferee++;
        	else if (r.getType() == RoleType.AUTHOR)
        		numberAuthor++;
        }
        
        model.put("numberChairman", numberChairman);
        model.put("numberPCMember", numberPCMember);
        model.put("numberReferee", numberReferee);
        model.put("numberAuthor", numberAuthor);
        model.put("listRole", listRole);
        
        logger.info("Get the list of articles of an author");
        List<Article> listAllArticle =  articleManager.getArticleOfAuthor(SessionService.getInstance().getCurrentUserLogin());
        Integer numberArticle = listAllArticle.size();
        
        //Map<Integer, Object> listConfForArticle = new HashMap<Integer, Object>();
        List<Conference> listConfForArticle = new ArrayList<Conference>();
        for (Article a : listAllArticle) {
        	listConfForArticle.add(articleManager.getConferenceForArticle(a.getId()));
        }
        
        model.put("listConfForArticle", listConfForArticle);
        model.put("numberArticle", numberArticle);
        model.put("listAllArticle", listAllArticle);
        
        logger.info("Get the list of conferences of an user");
        List<Conference> listConfForUser = conferenceManager.getConferences(SessionService.getInstance().getCurrentUser());
        Integer numberConference = listConfForUser.size();
        model.put("numberConference", numberConference);
        model.put("listConfForUser", listConfForUser);
        
        logger.info("Get the number of conferences the user can create");
        Integer numberCreationTokens = conferenceManager.conferenceCreationTokens(SessionService.getInstance().getCurrentUserLogin());
        model.put("numberCreationTokens", numberCreationTokens);
        
        return new ModelAndView("userBoard", model);
    }
}