package eu.dnetlib.enabling.aas.ismock;

import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.bidimap.DualHashBidiMap;
import org.springframework.util.StringUtils;

import eu.dnetlib.enabling.aas.service.A2Constants;
import eu.dnetlib.enabling.aas.utils.ResourcesGenerator;


/**
 * IS utilities for decoding/encoding resource ids.
 * @author mhorst
 *
 */
public class ISMockUtils {

	static public BidiMap colTypeToColName = new DualHashBidiMap();
	
	static {
		colTypeToColName.put(A2Constants.RESOURCE_TYPE_SECURITY_PROFILE, A2Constants.IS_COLLECTION_SECPROF_PATH);
		colTypeToColName.put(A2Constants.RESOURCE_TYPE_SECURITY_CONTEXT, A2Constants.IS_COLLECTION_SECCTX_PATH);
		colTypeToColName.put(A2Constants.RESOURCE_TYPE_SECURITY_POLICY, A2Constants.IS_COLLECTION_SECPOLICY_PATH);
//		for testing purposes
		colTypeToColName.put(ResourcesGenerator.RESOURCE_TYPE_DRIVER_TEST, ResourcesGenerator.IS_COLLECTION_DRIVER_TEST_PATH);
		colTypeToColName.put(ResourcesGenerator.RESOURCE_TYPE_USER_RESOURCE, ResourcesGenerator.IS_COLLECTION_DRIVER_USER_RESOURCE_PATH);
	}
	
	static String delimiter="$";
	
	/**
	 * Decodes complex profId to array of fileCol and resourceId.
	 * @param profId
	 * @return String[0] - fileColl, String[1] - fileName.
	 */
	public static String[] decodeProfileId(String profId) {
		if (profId==null)
			return null;
		String[] result = StringUtils.tokenizeToStringArray(profId, delimiter);
		if (result.length==2) {
			result[0] = (String) colTypeToColName.get(result[0]);
			return result;
		}else
			return null;
	}
	
	/**
	 * Encodes collectionName and resourceId to the profId.
	 * @param collectionName
	 * @param fileName
	 * @return profId
	 */
	public static String createProfileId(String fileColl, String fileName) {
		StringBuffer strBuff = new StringBuffer();
		strBuff.append(colTypeToColName.getKey(fileColl));
		strBuff.append(delimiter);
		strBuff.append(fileName);
		return strBuff.toString();
	}
	
}
