package routines;

/*
 * user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
 * it must be before the "{talendTypes}" key.
 * 
 * 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
 * long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
 * Short
 * 
 * 3. {Category} define a category for the Function. it is required. its value is user-defined .
 * 
 * 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
 * 
 * <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
 * Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
 * added. you can have many parameters for the Function.
 * 
 * 5. {example} gives a example for the Function. it is optional.
 */
public class LcagTestTools {

	private static LcagLogger log = LcagLogger.getLogger(LcagTestTools.class);
       
    /**
     * 
     * generateCode: Generate a sequence of codes from a given template @startCode.   
     * 
     * {talendTypes} String
     * 
     * {Category} LCAG-Test Tools
     * 
     * {param} Object("startCode") 		input: Code to start with, e.g. TAA
     * {param} Object("n")   			input: n-th code in generated sequence, e.g. TAC for n=2
     * 
     */
    public static String generateCode(String startCode, int n)
    {
    	String result = startCode;
    	char[] resultAry = startCode.toCharArray();
    	
    	for (int i = startCode.length()-1; i > 0; i -=1) {
    		char c = startCode.charAt(i);
    		int oldOrd =  (c >= 'a') ? (int)c - (int)'a' : (int)c - (int)'A';
    		int newOrd = oldOrd + n;
    		
    		int rest = newOrd % 26;
    		char newChar = (char)(c - oldOrd + rest);

    		n = newOrd / 26;
    		resultAry[i] = newChar;
    		
    		if (n == 0) break;
    	}

    	result = String.valueOf(resultAry);
    			
    	return result;
    }
    
    /**
     * 
     * compareToExpected: compares an expected result (first parameter) with the actual result (second paramter). Returns "OK" if both are equal and the actual value else. 
     * 
     * {talendTypes} String
     * 
     * {Category} LCAG-Test Tools
     * 
     * {param} Object("expectedResult") input: expected result object
     * {param} Object("ActualResult")   input: actual result object
     * 
     */
    public static String compareToExpected(Object expectedResult, Object actualResult)
    {
    	String result = "OK";
    	
    	if (! java.util.Objects.equals(expectedResult, actualResult)) {
			result = (actualResult == null) ? "n/a" : actualResult.toString();
    	}
    	
    	return result;
    }
    
    /**
     * 
     * compareToSimilarExpected: compares an expected String (first parameter) with an actual String (second parameter). Returns "OK" if both 
     * are equal excluding blancs and case and the actual value else. 
     * 
     * {talendTypes} String
     * 
     * {Category} LCAG-Test Tools
     * 
     * {param} String("expectedResult") input: expected result object
     * {param} String("ActualResult")   input: actual result object
     * 
     */
    public static String compareToSimilarExpected(String expectedResult, String actualResult)
    {
    	String result = "OK";
    	
    	if (actualResult == null && expectedResult != null) {
    		result = "n/a";
    	} else if (actualResult != null && expectedResult == null) {
    		result = actualResult;
    	} else if (actualResult != null && expectedResult != null) {
        	String expectedSimilarResult = expectedResult.replaceAll(" ", "");
        	String actualSimilarResult = actualResult.replaceAll(" ", "");

        	if (! expectedSimilarResult.equalsIgnoreCase(actualSimilarResult)) { 
        		result = actualResult;
        	}
    	}
    	
    	return result;
    }
    
}
