package org.yacos.web.author.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

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.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
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.conferences.Conference;
import org.yacos.core.users.User;
import org.yacos.web.author.form.FormSubmission;
import org.yacos.web.system.session.SessionService;

public class SArticleController extends SimpleFormController {

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

	private IArticleManager articleManager;
	private String downloadDirPath;

	public ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
	throws ServletException, IllegalStateException {

		// Fetching conference
		Conference conf = SessionService.getInstance().getCurrentConference();
		// Fetching user
		User user = SessionService.getInstance().getCurrentUser();
		
		String title = ((FormSubmission) command).getTitle();
		String theme = ((FormSubmission) command).getTheme();

		ArrayList<String> listSecondaryAuthor = new ArrayList<String>();
		// Removes null entries from the list
		for(String sAuthor : ((FormSubmission) command).getListe()){
			if(sAuthor != null){
				listSecondaryAuthor.add(sAuthor);
			}
		}
		
		String nomFile="";
		
		Article newArticle = articleManager.addArticle(
				title, theme, nomFile, user.getLogin(),
				listSecondaryAuthor, Article.State.SUMMARY, conf.getId());
		
		if(newArticle==null){
			return new ModelAndView(new RedirectView(getFormView()));
		}
		
		try {
			MultipartFile file = ((FormSubmission) command).getFile();
			if (file == null) {
				errors.rejectValue("file", "form.submitArticle.noFile");
				return new ModelAndView(new RedirectView(getFormView()));
			}
			else {

				byte b[] = file.getBytes();
				
				String path = "conference"+conf.getId()+"/";
				(new File(downloadDirPath+path)).mkdirs();
				path += newArticle.getId()+"_"+file.getOriginalFilename();
				File uploadedFile = new File(downloadDirPath+path);
				
				FileOutputStream fos = new FileOutputStream(uploadedFile);

				fos.write(b);
				fos.close();
				
				newArticle.setURL_article(path);
				articleManager.updateArticle(newArticle);
			}
		}
		catch (FileNotFoundException e) {
			System.out.println(e);
		} catch (IOException e) {
			System.out.println(e);
		}
		
		// The role author might have been added, refresh the credentials
		SessionService.getInstance().refreshAuthentication();
		
		return new ModelAndView(new RedirectView(getSuccessView()));
	}

	protected Object formBackingObject(HttpServletRequest request) throws ServletException {
		FormSubmission myArticle = new FormSubmission();
		myArticle.setTitle("");
		myArticle.setTheme("");
		//myArticle.setMainauthor("");
		myArticle.setSecondaryauthor("");
		return myArticle;
	}

	public IArticleManager getArticleManager() {
		return articleManager;
	}

	public void setArticleManager(IArticleManager articleManager) {
		this.articleManager = articleManager;
	}
	
	public String getDownloadDirPath() {
		return downloadDirPath;
	}
	
	public void setDownloadDirPath(String downloadDirPath) {
		this.downloadDirPath = downloadDirPath;
	}
}