package org.yacos.web.PCmember.controller;

import java.lang.reflect.UndeclaredThrowableException;

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.springframework.web.servlet.view.RedirectView;
import org.yacos.core.article.Article;
import org.yacos.core.article.IArticleManager;
import org.yacos.core.article.Article.State;
import org.yacos.core.conferences.Conference;
import org.yacos.core.users.User;
import org.yacos.web.system.controller.MailSenderService;
import org.yacos.web.system.session.SessionService;
import org.yacos.web.system.session.YACOSUtils;

public class ValidateOrRejectController extends SimpleFormController {

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

	private IArticleManager articleManager;

	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
	throws ServletException{

		logger.info("Returning ValidateOrReject view");
		String articleId = request.getParameter("id");

		try {
			Integer id = new Integer(articleId);

			Article a = articleManager.getArticle(id);
			
			if (a == null)
				throw new ModelAndViewDefiningException(new ModelAndView("404error"));
			
			String value = request.getParameter("value");

			Conference conf = SessionService.getInstance().getCurrentConference();
			String userLogin = a.getMainAuthor().getLogin();
			User author = SessionService.getInstance().getUserManager().getUser(userLogin);
			if (value.equals("valide"))	{
				a.setState(State.ACCEPTED);
				// Sending email
				String mailSubject = "YACOS : Your article has been accepted";
				String mailBody = "Your article entitled "+a.getTitle();
				mailBody += " has been accepted by the program committee.\n";
				mailBody += "It's content will be featured on the program of the upcoming conference :\n";
				mailBody += conf.getTitle()+"\n";
				mailBody += "Please mark on your calendar the dates for this conference :";
				mailBody += "Starts on "+conf.getDateStart()+" and ends on "+conf.getDateEnd()+"\n";
				mailBody += "Thank you for your participation.\n";
				MailSenderService.getInstance().sendEMail(author.getEmail(), mailSubject, mailBody);
			}
			else if (value.equals("reject")) {
				a.setState(State.REJECTED);
				// Sending email
				String mailSubject = "YACOS : Your submission has been declined";
				String mailBody = "Your article entitled "+a.getTitle();
				mailBody += " has not been accepted by the program committee.\n";
				mailBody += "You can view the comments leaved by the evaluators on your personnal space :\n";
				mailBody += YACOSUtils.fullURL("userBoard.htm")+"\n";
				mailBody += "Thank you for your participation.\n";
				MailSenderService.getInstance().sendEMail(author.getEmail(), mailSubject, mailBody);
			}
			else
				throw new ModelAndViewDefiningException(new ModelAndView("404error"));

			try {
				articleManager.updateArticle(a);
			} catch (UndeclaredThrowableException e) {
				e.printStackTrace();
			}
		}
		catch (NumberFormatException e) {
			throw new ModelAndViewDefiningException(new ModelAndView("404error"));
		}
		
		return new ModelAndView(new RedirectView("validateArticle.htm"));
	}

	public IArticleManager getArticleManager() {
		return articleManager;
	}

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