package eu.dnetlib.social.web;

import java.io.IOException;
import java.util.Collection;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.xml.sax.SAXException;

import com.google.common.collect.Lists;
import com.google.gdata.util.ServiceException;

import eu.dnetlib.social.SocialUploadable;
import eu.dnetlib.social.YouTubeObject;
import eu.dnetlib.social.producer.SRWReader;
import eu.dnetlib.social.producer.SocialEnqueuer;

/*
 * SocialController is a MultiActionController. 
 * The MultiActionController class is defined in package org.springframework.web.servlet.mvc.multiaction and it is
 * capable of mapping requests to method names and then invoking the correct method to handle a particular request. 
 *
 * Using the MultiActionController is especially handy when you have a lot of related functionality that would 
 * perhaps be nice to define all in a single class without having to implement one Controller for each bit of functionality. 
 * 
 * The MultiActionController typically is not appropriate for capturing very complex request-handling logic or use cases 
 * that address totally-different areas of functionality, and you are encouraged to stick with the standard 
 * 'one piece-of-functionality maps to one Controller' for such cases.
 */
/**
 * Main controller for the social site publishing.
 * 
 * @author alessia
 * 
 */
public class SocialController {
	private static final Log log = LogFactory.getLog(SocialController.class);

	private SRWReader srwReader;
	private SocialEnqueuer enqueuer;

	/** First page. */
	@RequestMapping("index.do")
	public void index(final ModelMap map) {
		log.debug("index.do: hello");
	}

	/**
	 * Monitoring page.
	 * 
	 * @throws ParserConfigurationException
	 *             parsing srw document
	 * @throws IOException
	 *             parsing srw document
	 * @throws SAXException
	 *             parsing srw document
	 * @throws XPathExpressionException
	 *             parsing srw document
	 * @throws ServiceException
	 */
	@RequestMapping("publish.do")
	public void publish(
			@RequestParam(value = "username", required = false) final String username,
			@RequestParam(value = "pwd", required = false) final String pwd,
			@RequestParam(value = "monitor", required = false) final Boolean monitor,
			final ModelMap map) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException, ServiceException {
		if (monitor == null || !monitor) {
			log.debug(".publish: will redirect to the monitoring page. Some info has to be set...");
			log.info("Reading file...");
			Collection<YouTubeObject> toPublish = this.srwReader.extractFromDocument();
			log.info("done");
			Collection<SocialUploadable> objects = Lists.newArrayList();
			objects.addAll(toPublish);
			for (YouTubeObject o : toPublish)
				this.enqueuer.produce(o);
			log.info("Monitoring status...");
		} else {
			map.addAttribute("message", "Monitoring upload progress: coming soon!");
		}
	}

	@Required
	public void setSrwReader(final SRWReader srwReader) {
		this.srwReader = srwReader;
	}

	public SRWReader getSrwReader() {
		return srwReader;
	}

	public void setEnqueuer(final SocialEnqueuer enqueuer) {
		this.enqueuer = enqueuer;
	}

	public SocialEnqueuer getEnqueuer() {
		return enqueuer;
	}

	//	public void setUploader(YouTubeResumableUploader uploader) {
	//		this.uploader = uploader;
	//	}
	//
	//	public YouTubeResumableUploader getUploader() {
	//		return uploader;
	//	}

	//	public void setPublisher(final YouTubePublisher publisher) {
	//		this.publisher = publisher;
	//	}
	//
	//	public YouTubePublisher getPublisher() {
	//		return publisher;
	//	}

	//	@Required
	//	public void setPublisher(YouTubePublisher publisher) {
	//		this.publisher = publisher;
	//	}
	//
	//	public YouTubePublisher getPublisher() {
	//		return publisher;
	//	}

}
