package org.yacos.web.PCmember.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.Article;
import org.yacos.core.article.IArticleManager;
import org.yacos.core.article.Preference;
import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.users.IUserManager;
import org.yacos.core.users.User;
import org.yacos.web.PCmember.form.FormPreference;
import org.yacos.web.system.controller.NoConferenceSelectedException;
import org.yacos.web.system.session.SessionService;

public class ChoosePreferenceController  extends SimpleFormController {

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

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


	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		Integer currentConferenceId = SessionService.getInstance().getCurrentConferenceId();
		
		if(currentConferenceId == null){
			throw new NoConferenceSelectedException();
		}
		
		List<Article> articleList=articleManager.getArticles(currentConferenceId);
		
		request.getSession().setAttribute("articleList", articleList);
		
		User currentUser = SessionService.getInstance().getCurrentUser();
		
		ArrayList<Preference> preferencesList = new ArrayList<Preference>();
		Preference pref;
		for(Article article : articleList){
			pref = articleManager.getPreferenceForUserAndArticle(article.getId(), currentUser.getLogin());
			if(pref == null){
				pref = new Preference();
				pref.setArticle(article);
				pref.setPcMember(currentUser);
				pref.setPreferenceType(Preference.PreferenceType.INDIFFERENT);
			}
			preferencesList.add(pref);
		}

		FormPreference preferences = new FormPreference();
		preferences.setPreferences(preferencesList);
		
		return preferences;
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
	throws Exception {

		FormPreference  fp=(FormPreference) command;

		List<Preference> listPreference=fp.getPreferences();
		
		for(Preference preference : listPreference){
			articleManager.addOrUpdatePreference(preference.getArticle().getId(),
					preference.getPcMember().getLogin(),
					preference.getPreferenceType());
		}
		
		Map<String, String> model = new HashMap<String, String>();
		model.put("message", "done");
		
		return new ModelAndView(new RedirectView(getSuccessView()), model);
	}
	
	

	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;
	}
}