package eu.dnetlib.enabling.aas.is;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.ws.wsaddressing.W3CEndpointReference;
import javax.xml.ws.wsaddressing.W3CEndpointReferenceBuilder;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import eu.dnetlib.enabling.tools.JaxwsServiceResolverImpl;

/**
 * Information Service utils class.
 * @author mhorst
 *
 */
public class ISUtils {
	
	protected static final Logger log = Logger.getLogger(ISUtils.class);

	/**
	 * Builds EPR for given url. WSDL location is generated automatically.
	 * @param url
	 * @return EPR for given url
	 */
	public static W3CEndpointReference buildEPR(String url) {
		return buildEPR(url, url + "?wsdl");
	}
	
	/**
	 * Builds EPR for given url and wsdl.
	 * @param url
	 * @param wsdl
	 * @return EPR for given url and wsdl
	 */
	public static W3CEndpointReference buildEPR(String url, String wsdl) {
		W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder();
		builder.address(url);
		builder.wsdlDocumentLocation(wsdl);
		return builder.build();
	}
	
	/**
	 * Extracts ResultSet identifier from {@link W3CEndpointReference} response.
	 * @param eprType
	 * @return ResultSet identifier.
	 */
	public static String extractResultSetId(W3CEndpointReference eprType) {
		if (eprType!=null) {
//			TODO it might be faster do instantiate JaxwsServiceResolverImpl only once
			return new JaxwsServiceResolverImpl().getResourceIdentifier(eprType);
		} else {
			return null;
		}
	}
	
	/**
	 * Extracts ResultSet identifier from ResultSet xml-type response.
	 * @param sourceResultSetId
	 * @return ResultSet identifier.
	 */
	public static String extractResultSetId(String sourceResultSetId) {
		if (sourceResultSetId==null || sourceResultSetId.length()==0)
			return null;
        DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);
        factory.setValidating(false);
        DocumentBuilder db;
		try {
			db = factory.newDocumentBuilder();
			Document doc = db.parse(new InputSource(new StringReader(sourceResultSetId)));
			Element documentElement = doc.getDocumentElement();
	    	NodeList nodeList = documentElement.getElementsByTagName("driver:ResourceIdentifier");
	    	if (nodeList.getLength()!=1) {
	    		log.error("Invalid list of nodes for driver:ResourceIdentifier element. Expected 1, found: "+nodeList.getLength());
	    		return null;
	    	}
	    	
	    	if (nodeList.item(0)==null) {
	    		log.error("Couldn't find driver:ResourceIdentifier element!");
	    		return null;
	    	}
	    	return getStringFromNode(nodeList.item(0));
	    	
		} catch (Exception e) {
			log.error("Exception occured when extracting ResultSet id from ResultSet service xml-type response!",e);
			return null;
		}

	}
	
	
	private static String getStringFromNode(Node node)	{
	    
//		This code may not work on some jdk
//		Element resourceIdentifier = (Element) node;
//    	return resourceIdentifier.getTextContent();
		
		/*
		try {
			DOMSource domSource = new DOMSource(node);
			StringWriter writer = new StringWriter();
			StreamResult result = new StreamResult(writer);
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			transformer.transform(domSource, result);
			return writer.toString();
		} catch (TransformerException e) {
			log.error("Exception occured when transforming node value!", e);
			return null;
		}
		*/
		
		return node.getFirstChild().getNodeValue(); 
	}

	
}
