package eu.dnetlib.enabling.aas.ismock;

import java.util.List;

import javax.xml.ws.wsaddressing.W3CEndpointReference;

import org.apache.log4j.Logger;

import eu.dnetlib.common.rmi.UnimplementedException;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
import eu.dnetlib.enabling.is.store.rmi.ISStoreException;
import eu.dnetlib.enabling.is.store.rmi.ISStoreService;


/**
 * Mock class implementing ISLookUp service functionalities.
 * @author mhorst
 *
 */
public class MockISLookUp implements ISLookUpService {
	
	public static final String IDENTIFY = "IS_LookUp-mock-0.9.1";

	protected static final Logger log = Logger.getLogger(MockISLookUp.class);
	
	private ISStoreService storeService;
	

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#getResourceProfile(java.lang.String)
	 */
	public String getResourceProfile(String profId) throws ISLookUpException {
		if (profId==null)
			return null;
		String[] decodedId = ISMockUtils.decodeProfileId(profId);
		if (decodedId==null)
			return null;
		try {
			String collectionName = decodedId[0];
			String resourceId = decodedId[1];
			String xmlContent = storeService.getXML(resourceId, collectionName);
			return xmlContent;
		} catch (ISStoreException e) {
			throw new ISLookUpException(e);
		}
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#getResourceQoSParams(java.lang.String)
	 */
	public String getResourceQoSParams(String id) {
		throw new UnimplementedException();
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#getResourceTypeSchema(java.lang.String)
	 */
	public String getResourceTypeSchema(String resourceType) {
		throw new UnimplementedException();
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#listCommunities()
	 */
	public W3CEndpointReference listCommunities() {
		throw new UnimplementedException();
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#listCollections(java.lang.String, java.lang.String, java.lang.String)
	 */
	public W3CEndpointReference listCollections(String format, String idfather, String owner) {
		throw new UnimplementedException();
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#listDHNIDs()
	 */
	public List<String> listDHNIDs() {
		throw new UnimplementedException();
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#listResourceProfiles(java.lang.String, java.lang.String, java.lang.String)
	 */
	public W3CEndpointReference listResourceProfiles(
			String resourceKind, String format, String resourceType) throws ISLookUpException {
		try {
			if (resourceKind==null || resourceType==null)
				return null;
			String resourceCollectionName = extractCollectionName((String) ISMockUtils.colTypeToColName.get(resourceType));
			if (resourceCollectionName==null)
				return null;
			String query = "for $el in fn:collection(\""+resourceCollectionName+"\") return $el";
			return storeService.searchXML(query);
		} catch (ISStoreException e) {
			throw new ISLookUpException(e);
		}
	}

	private String extractCollectionName(String fileColl) {
		if (fileColl==null)
			return null;
		return fileColl.substring("db/".length());
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#searchProfile(java.lang.String)
	 */
	public W3CEndpointReference searchProfile(String xQuery) throws ISLookUpException {
		try {
			return storeService.searchXML(xQuery);
		} catch (ISStoreException e) {
			throw new ISLookUpException(e);
		}
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#getCollection(java.lang.String, java.lang.String)
	 */
	public String getCollection(String profId, String format) {
		throw new UnimplementedException();
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#retrieveCollection(java.lang.String)
	 */
	public String retrieveCollection(String profId) throws ISLookUpException {
		throw new UnimplementedException();
	}
	
	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#getResourceProfileByQuery(java.lang.String)
	 */
	public String getResourceProfileByQuery(String XQuery) throws ISLookUpException {
		try {
			return storeService.getXMLbyQuery(XQuery);
		} catch (ISStoreException e) {
			throw new ISLookUpException(e);
		}
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#listServiceIDs(java.lang.String)
	 */
	public List<String> listServiceIDs(String serviceType) {
		throw new UnimplementedException();
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#listServiceTypes()
	 */
	public List<String> listServiceTypes() {
		throw new UnimplementedException();
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.common.rmi.BaseService#notify(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public void notify(String subscrId, String topic, String isId, String message) {
		throw new UnimplementedException();
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#flushCachedResultSets()
	 */
	public Boolean flushCachedResultSets() {
		throw new UnimplementedException();
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#listResourceTypes()
	 */
	public List<String> listResourceTypes() {
		throw new UnimplementedException();
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.common.rmi.BaseService#identify()
	 */
	public String identify() {
		return MockISLookUp.IDENTIFY;
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#quickSearchProfile(java.lang.String)
	 */
	public List<String> quickSearchProfile(String XQuery) {
		throw new UnimplementedException();
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#listMDStores(java.lang.String)
	 */
	public W3CEndpointReference listMDStores(String metadataformat) throws ISLookUpException {
		throw new UnimplementedException();
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.common.rmi.BaseService#start()
	 */
	public void start() {
	}
	
	/**
	 * Gets ISStore service.
	 * @return
	 */
	public ISStoreService getStoreService() {
		return storeService;
	}

	/**
	 * Sets up ISStore service.
	 * @param storeService
	 */
	public void setStoreService(ISStoreService storeService) {
		this.storeService = storeService;
	}

}
