package eu.dnetlib.social.producer;

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

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.google.common.collect.Lists;

import eu.dnetlib.social.YouTubeObject;

/**
 * @author alessia
 * 
 */
public class SRWReader {
	private static final Log log = LogFactory.getLog(SRWReader.class);

	private String srwPathFile;
	private String userName, pwd;

	public Collection<eu.dnetlib.social.YouTubeObject> extractFromDocument() throws XPathExpressionException, SAXException, IOException,
			ParserConfigurationException {
		Collection<YouTubeObject> objects = Lists.newArrayList();
		Document srwDoc;
		srwDoc = this.loadFromFile();
		XPath xpath = XPathFactory.newInstance().newXPath();
		//this is just because demo data has no mime types.
		String mimeType = "video/mpg";
		String[] categories = new String[] { "Nonprofit" };
		NodeList recordList = (NodeList) xpath.evaluate("//record", srwDoc, XPathConstants.NODESET);
		for (int i = 0; i < recordList.getLength(); i++) {
			Element r = (Element) recordList.item(i);
			YouTubeObject o = new YouTubeObject();
			o.setTask("eu.dnetlib.social.consumer.YouTubeSocialDequeuer");
			o.setUserName(userName);
			o.setPassword(pwd);
			String pid = ((Element) (r.getElementsByTagName("iisg:isShownBy").item(0))).getTextContent();
			String title = ((Element) (r.getElementsByTagName("dc:title").item(0))).getTextContent();
			String descr = ((Element) (r.getElementsByTagName("dc:description").item(0))).getTextContent();
			String downloadURL = ((Element) (r.getElementsByTagName("iisg:isShownBy").item(0))).getTextContent();
			NodeList keys = r.getElementsByTagName("dc:subject");
			String[] keywords = new String[keys.getLength() + 1];
			keywords[0] = "hope-proof-of-concept";
			for (int j = 0; j < keys.getLength(); j++) {
				keywords[j + 1] = ((Element) keys.item(j)).getTextContent();
			}
			//PID is used for the name
			o.setName(pid);
			o.setCategories(categories);
			o.setDescription(descr);
			o.setKeywords(keywords);
			o.setMimeType(mimeType);
			o.setPID(pid);
			o.setTitle(title);
			o.setDownloadURL(downloadURL);
			objects.add(o);
			log.debug("created YouTubeObject with pid = " + pid);
		}
		return objects;
	}

	protected Document loadFromFile() throws SAXException, IOException, ParserConfigurationException {
		//System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
		DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
		log.debug("Using doc builder factory:" + f.getClass());
		log.debug("Looking srw in: " + this.srwPathFile);
		return f.newDocumentBuilder().parse(new FileInputStream(this.srwPathFile));
	}

	public void setSrwPathFile(final String srwPathFile) {
		this.srwPathFile = srwPathFile;
	}

	public String getSrwPathFile() {
		return srwPathFile;
	}

	public void setUserName(final String userName) {
		this.userName = userName;
	}

	public String getUserName() {
		return userName;
	}

	public void setPwd(final String pwd) {
		this.pwd = pwd;
	}

	public String getPwd() {
		return pwd;
	}

}
