package hw4;

import javax.swing.SwingConstants;

/**
 * A utility class for working with text.
 *
 * @author P. Parrot, Lexicality, Inc.
 * @version 1.0
 */
public class TextUtils {

    /**
     * Private constructor included because this is a utility class. 
     */
    private TextUtils() {
    }

    /**
     * Find the needle in the haystack. Return the 0-based
     * index if it is present and -1 if it is not.
     *
     * @param needle The char to look for
     * @param haystack The array of char to look in
     * @return The index (or -1)
     */
    public static int findIndexOf(char needle, char[] haystack) {
        for (int i = 0; i < haystack.length && needle >= haystack[i]; i++) {
            if (needle == haystack[i]) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Find the replacement character for a particular character based on
     * its index in an array of characters. 
     *
     * @param needle The character to be replaced
     * @param haystack The array to use to determine the replacement
     * @param replacements The replacement characters
     * @return The replacement character (or the original character, if not found)
     * @throws IllegalArgumentException If the arrays aren't conformal
     */
    public static char findReplacementFor(char needle, char[] haystack, char[] replacements) 
            throws IllegalArgumentException {
        if (replacements.length != haystack.length) {
            throw new IllegalArgumentException("haystack and replacements not conformal");
        }

        int index = TextUtils.findIndexOf(needle, haystack);
        if (index <= 0) return needle;
        else return replacements[index];
    }

    /**
     * Return a String with a length of desiredLength. If necessary, remove characters
     * from the right. If necessary, add characters either to the left or right.
     *
     * @param s The String of interest
     * @param padding The character to add (if needed)
     * @param side The side to add to (if needed)
     * @param desiredLength The desire length of the result
     * @return The resulting String
     */
    public static String pad(String s, char padding, int side, int desiredLength) {

        String result;
        String spad;
        spad = "" + padding;

        int padSize = desiredLength - s.length();
        if (side == SwingConstants.RIGHT) {
            result = s + spad.repeat(padSize);
        } else {
            result = spad.repeat(padSize) + s;
        }
        result = result.substring(0, desiredLength);

        return result;
    }

    /**
     * Remove all occurrences of one or more characters from a given String.
     * Note: This method is case-sensitive.
     *
     * @param s The String
     * @param chars The characters to remove
     * @return The String with the characters removed
     */
    public static String removeFrom(String s, char[] chars) {
        String result = "" + s.charAt(0);
        for (int i = 1; i < s.length(); i++) {
            char c = s.charAt(i);
            int index = TextUtils.findIndexOf(c, chars);
            if (index < 0) {
                result += c;
            }
        }
        return result;
    }

}
