package org.yacos.web.PCmember.controller;

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.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
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.forum.IMessageManager;
import org.yacos.core.forum.Message;
import org.yacos.web.PCmember.form.FormArticleMessage;
import org.yacos.web.system.session.SessionService;


public class ForumArticleController extends SimpleFormController {

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

	private IMessageManager messageManager;
	private IArticleManager articleManager;

	public IMessageManager getMessageManager() {
		return messageManager;
	}

	public void setMessageManager(IMessageManager messageManager) {
		this.messageManager = messageManager;
	}

	public IArticleManager getArticleManager() {
		return articleManager;
	}

	public void setArticleManager(IArticleManager articleManager) {
		this.articleManager = articleManager;
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		
		logger.info("Returning message view");

		FormArticleMessage fam = new FormArticleMessage();
		
		return fam;
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		
		Map<String, Object> model = new HashMap<String, Object>();
		
		String articleId = request.getParameter("articleId");

		try {
			Integer id = new Integer(articleId);
			Article a = articleManager.getArticle(id);

			if (a == null)
				throw new ModelAndViewDefiningException(new ModelAndView("404error"));

			List<Message> listMessage = messageManager.getMessagesForArticle(id);
			
			model.put("listMessage", listMessage);
			
			return model;
			
		}
		catch (NumberFormatException e) {
			throw new ModelAndViewDefiningException(new ModelAndView("404error"));
		}
		
		
	}

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

		String title = ((FormArticleMessage) command).getTitle();
		String message = ((FormArticleMessage) command).getMessage();

		String articleId = request.getParameter("articleId");

		try {

			Integer id = new Integer(articleId);
			Article a = articleManager.getArticle(id);

			if (a == null)
				throw new ModelAndViewDefiningException(new ModelAndView("404error"));

			messageManager.addMessage(message, title, id, SessionService.getInstance().getCurrentUserLogin());

			return new ModelAndView(new RedirectView("forumArticle.htm?articleId=" + id));

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