/**
 * ParameterReferenceDisplayHelper.java
 * Created on May 20, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.job.util;

/**
 * @author spuranik
 *
 * This class is used to get/break the parameter reference 
 * string used for a given menu option under a group 
 * at job creation time.
 */
public class ParameterReferenceHelper {

	public static String PROPERTY_REFERENCE_ESCAPED_PREFIX = "\\$";
	public static String GROUPNAME_DELIMETER = ".";
	public static String PROPERTY_REFERENCE_FORMAT_DELIMITER = ".";
	public static String PROPERTY_REFERENCE_PREFIX = "$";

	public static String NOTHING = "";

	/**
	 * @param groupName
	 * 	the group name under which this property is available
	 * @param property
	 * 	the property name 
	 * @return
	 * 	a string of the format: $[top level group name].[property name]
	 */
	public static String buildParameterReferenceProperty(String groupName, String propertyName) {
		return PROPERTY_REFERENCE_PREFIX + groupName + PROPERTY_REFERENCE_FORMAT_DELIMITER
				+ propertyName;
	}

	/**
	 * @param parameterReferenceProperty
	 * 		the property reference formated string for a property
	 * @return
	 * 		the property name included in the given string 
	 */
	public static String getPropertyName(String parameterReferenceProperty) {
		// doing this because there could be multi-level groups separated by '.' 
		int index = parameterReferenceProperty.lastIndexOf(PROPERTY_REFERENCE_FORMAT_DELIMITER);
		if (index != -1)
			return parameterReferenceProperty.substring(index + 1);

		return "";
	}

	/**
	 * The input could be of the form 'parentGroup.childGroup' or 'groupname'. In the first case return is
	 * 'parentGroup' and the second case 'groupname'
	 * 
	 * @param parameterReferenceProperty
	 * 		the property reference formated string for a property
	 * @return
	 * 		the group name included in the given string 
	 */
	public static String getGroupName(String parameterReferenceProperty) {
		int index = parameterReferenceProperty.lastIndexOf(PROPERTY_REFERENCE_FORMAT_DELIMITER);
		if (index != -1) {
			String groupName = parameterReferenceProperty.substring(0, index);
			groupName = groupName.replaceAll(PROPERTY_REFERENCE_ESCAPED_PREFIX, NOTHING);
			return groupName;
		}

		return parameterReferenceProperty;
	}

	/**
	 * this method checks if the property starts with a $ which means its a 
	 * parameter reference. 
	 * 
	 * @param name
	 * 	name which needs to be inspected
	 * @return
	 * 	true/false
	 */
	public static boolean isParameterReference(String name) {
		if (name != null)
			return name.startsWith(PROPERTY_REFERENCE_PREFIX);
		else
			return false;
	}

	/**
	 * This method is used to get the leaf level group name from the given group name
	 * This is helpful because the group name could be multi-leveled 
	 * 
	 * @param groupName
	 * 	group name comprised of all group level names e.g. Title.ADI
	 * @return
	 *  the leaf level group name (e.g ADI) if in the right format else returns the same string 
	 *  that it recvd.
	 */
	public static String getLeafGroup(String groupName) {
		// the leaf level here really means second level :(
		// because we have to support "." in the spec name, cannot do last index of 
		// "." as it will return an incomplete string and break things.
		int index = groupName.indexOf(GROUPNAME_DELIMETER);
		if (index != -1)
			return groupName.substring(index + 1);

		return groupName;
	}

	/**
	 * this method separates the root group from any child groups
	 * that are concatenated in the groupname
	 * 
	 * @param groupName
	 * 	groupname from which parent group name needs to be extracted
	 * @return
	 * 	parent group name (which appears before the "." in teh given group name)
	 */
	public static String getParentGroup(String groupName) {
		// the leaf level here really means second level :(
		// because we have to support "." in the spec name, cannot do last index of 
		// "." as it will return an incomplete string and break things.
		int index = groupName.indexOf(GROUPNAME_DELIMETER);
		if (index != -1)
			return groupName.substring(0, index);

		return groupName;
	}

	/**
	 * this is a convenience method used to combine multi-level group names to 
	 *  
	 * @param parentGroupName
	 * @param childGroupName
	 * @return
	 * 	concatenated string with the parent group name prefixed to the child group name
	 */
	public static String combineGroupNames(String parentGroupName, String childGroupName) {
		return parentGroupName + GROUPNAME_DELIMETER + childGroupName;
	}
}
