package eu.dnetlib.data.textengine.ws;

import javax.xml.ws.wsaddressing.W3CEndpointReference;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.dom4j.io.DocumentResult;

import eu.dnetlib.common.utils.XMLException;
import eu.dnetlib.common.utils.XMLUtils;
import eu.dnetlib.common.ws.dataprov.DataProviderProperties;
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;

public class TextEngineDataProviderProperties extends DataProviderProperties{


	private static final Log log = LogFactory.getLog(TextEngineDataProviderProperties.class);
	private String whiteList;
	private String blacklist;
	private String rsType;
	private W3CEndpointReference epr;
	private String rsId;
	
	private int totalNumberOfResults;
	
	
	public TextEngineDataProviderProperties(int expiryTime) {
		super(expiryTime);
	}
	
	/**
	 * @param whiteList the whiteList to set
	 */
	public void setWhiteList(String whiteList) {
		this.whiteList = whiteList;
	}
	/**
	 * @return the whiteList
	 */
	public String getWhiteList() {
		return whiteList;
	}
	/**
	 * @param blacklist the blacklist to set
	 */
	public void setBlacklist(String blacklist) {
		this.blacklist = blacklist;
	}
	/**
	 * @return the blacklist
	 */
	public String getBlacklist() {
		return blacklist;
	}
	/**
	 * @param rsType the rsType to set
	 */
	public void setRsType(String rsType) {
		this.rsType = rsType;
	}
	/**
	 * @return the rsType
	 */
	public String getRsType() {
		return rsType;
	}
	/**
	 * @param epr the epr to set
	 */
	public void setEpr(W3CEndpointReference epr) {
		this.epr = epr;
	}
	/**
	 * @return the epr
	 */
	public W3CEndpointReference getEpr() {
		return epr;
	}
	
	protected ResultSetService getResultSetService(){
		if (epr == null){
			log.debug("epr is null");
			return null;
		}else{
			W3CEndpointReference epRef = getEpr();
			DocumentResult infoset = new DocumentResult();
			epRef.writeTo(infoset);
			String rsLocation = null;
			try {
				rsLocation = XMLUtils.evaluate(infoset.getDocument(), "//*[local-name()='Address']");
				rsId = XMLUtils.evaluate(infoset.getDocument(), "//ResourceIdentifier:ResourceIdentifier/text()");
				log.debug("rsLocation: " + rsLocation);
				log.debug("rsId: " + rsId);
			} catch (XMLException e) {
				log.debug(e);
			}
			JaxWsProxyFactoryBean wsProxyFactory = new JaxWsProxyFactoryBean();
			wsProxyFactory.setServiceClass(ResultSetService.class);
			wsProxyFactory.setAddress(rsLocation);
			return (ResultSetService)wsProxyFactory.create();
		}
	}
	
	protected String getRsResourceIdentifier(){
		return rsId;
	}
	
	protected String getRsMode(){
		String mode = "non-waiting";
		if (getRsType().equals("pull")){
			mode = "waiting";
		}
		return mode;
	}
	
	@Override
	public int getNumberOfCacheResults() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public int getTotalNumberOfResults() {
		return totalNumberOfResults;
	}
	
	/*
	 * unused
	 * @see eu.dnetlib.common.ws.dataprov.DataProviderProperties#getType()
	 */
	public Type getType() {
		return null;
	}
	
	public void setTotalNumberOfResults(int totalNumberOfResults) {
		this.totalNumberOfResults = totalNumberOfResults;
	}
		
}
