package eu.dnetlib.contract.mocks;

import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.ws.wsaddressing.W3CEndpointReference;
import javax.xml.ws.wsaddressing.W3CEndpointReferenceBuilder;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import eu.dnetlib.enabling.aas.service.AASConstants;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;

public class ISLookUpMock implements ISLookUpService {

	@Override
	public Boolean flushCachedResultSets() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCollection(String arg0, String arg1)
			throws ISLookUpException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getResourceProfile(String arg0) throws ISLookUpException,
			ISLookUpDocumentNotFoundException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getResourceProfileByQuery(String arg0)
			throws ISLookUpException, ISLookUpDocumentNotFoundException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getResourceQoSParams(String arg0) throws ISLookUpException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getResourceTypeSchema(String arg0) throws ISLookUpException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public W3CEndpointReference listCollections(String arg0, String arg1,
			String arg2) throws ISLookUpException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public W3CEndpointReference listCommunities() throws ISLookUpException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> listDHNIDs() throws ISLookUpException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public W3CEndpointReference listMDStores(String arg0)
			throws ISLookUpException {
		// TODO Auto-generated method stub
		return null;
	}

	protected W3CEndpointReference buildEPR(String rsId) throws ParserConfigurationException {
		final W3CEndpointReferenceBuilder W3CResultSetEPR = new W3CEndpointReferenceBuilder();
		W3CResultSetEPR.address("http://localhost/resultSet");
		W3CResultSetEPR.serviceName(
				new QName("http://www.w3.org/2006/02/addressing/wsdl","theResultSet"));
		W3CResultSetEPR.endpointName(
				new QName("http://www.driver.org/schema", "theResultSet"));
		W3CResultSetEPR.wsdlDocumentLocation("http://localhost/resultSet?wsdl");
		final Document doc = DocumentBuilderFactory.newInstance()
				.newDocumentBuilder().newDocument();
		final Element referenceElement = doc.createElementNS(
				"http://www.driver.org", "ResourceIdentifier:ResourceIdentifier");
		referenceElement.setTextContent(rsId);
		W3CResultSetEPR.referenceParameter(referenceElement);
		return W3CResultSetEPR.build();
	}
	
	@Override
	public W3CEndpointReference listResourceProfiles(String arg0, String arg1,
			String arg2) throws ISLookUpException {
		if (arg0.equals(AASConstants.RESOURCE_KIND_SECURITY_POLICY) &&
				arg1==null &&
				arg2.equals(AASConstants.RESOURCE_TYPE_SECURITY_POLICY)) {
			try {
				return buildEPR(ResultSetMock.predefinedPoliciesRSId);
			} catch (ParserConfigurationException e) {
				throw new ISLookUpException(e);
			}
		} else {
			return null;
		}
	}

	@Override
	public List<String> listResourceTypes() throws ISLookUpException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> listServiceIDs(String arg0) throws ISLookUpException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> listServiceTypes() throws ISLookUpException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> quickSearchProfile(String arg0)
			throws ISLookUpException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String retrieveCollection(String arg0) throws ISLookUpException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public W3CEndpointReference searchProfile(String arg0)
			throws ISLookUpException {
		if (arg0.contains(AASConstants.RESOURCE_TYPE_SECURITY_PROFILE)) {
//			looking for userProf mode
			try {
				return buildEPR(ResultSetMock.predefinedUserSecProfRSId);
			} catch (ParserConfigurationException e) {
				throw new ISLookUpException(e);
			}
		} else {
			return null;
		}
	}

	@Override
	public String identify() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void notify(String arg0, String arg1, String arg2, String arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void start() {
		// TODO Auto-generated method stub

	}

}
