

package gov.va.med.cds.util;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.codec.binary.Base64;


/**
 * Strips out new lines in a given string.
 * 
 * 
 */
public class StringUtil
{
    /**
     * Strips out new lines in a given string.
     * 
     * @param source String with new lines.
     * @return String with no new lines.
     */
    public static String stripNewlines( String source )
    {
        if ( source == null )
        {
            return null;
        }
        StringBuilder buff = new StringBuilder( source.length() );
        char c;
        for ( int i = 0; i < source.length(); i++ )
        {
            c = source.charAt( i );
            if ( c != '\n' )
            {
                buff.append( c );
            }
        }

        return buff.toString();
    }


    public static String[] concat( String[] a, String[] b )
    {
        if ( a == null )
        {
            return b;
        }
        else if ( b == null )
        {
            return a;
        }
        else
        {
            String[] result = new String[a.length + b.length];

            int i = 0;
            for ( String element : a )
            {
                result[i++ ] = element;
            }

            for ( String element : b )
            {
                result[i++ ] = element;
            }

            return result;

        }
    }


    /**
     * Returns the string after the last occurrence of the given search char from the given string.
     * 
     * @param stringData
     * @param searchDelimiter
     * @return String
     * @see gov.va.med.cds.persistence.hibernate.HibernateConstraintViolationException( ConstraintViolationException
     *      cause)
     */
    public static String getLastStringAfterTheDelimiter( String stringData, char searchDelimiter )
    {
        return stringData.substring( stringData.lastIndexOf( searchDelimiter ) + 1, stringData.length() );
    }


    /**
     * Compare the two string while ignoring any whitespace found.
     * 
     * @param str1 String 1 to compare.
     * @param str2 String 2 to compare.
     * @return True if the two string don't differ or differ by white space only; false otherwise.
     */
    public static boolean equalsIgnoreWhiteSpace( String str1, String str2 )
    {
        boolean result = false;
        //first remove any ending white space if file/strings
        char[] c1 = str1.trim().toCharArray();
        char[] c2 = str2.trim().toCharArray();
        int c1Length = c1.length;
        int c2Length = c2.length;

        for ( int c1i = 0, c2i = 0; c1i < c1Length && c2i < c2Length; c1i++ , c2i++ )
        {
            while ( c1i < c1Length && Character.isWhitespace( c1[c1i] ) )
            {
                c1i++ ;
            }

            while ( c2i < c2Length && Character.isWhitespace( c2[c2i] ) )
            {
                c2i++ ;
            }

            if ( c1i == c1Length - 1 && c2i == c2Length - 1 )
            {
                result = true;
            }

            if ( c1[c1i] != c2[c2i] )
            {
                break;
            }
        }

        return result;
    }


    /**
     * Split and sort the split substrings and return a newly constructed <String> from the sorted order of split
     * substrings.
     * 
     * @param stringToSort <String> needing sorted
     * @param splitterExp <RegExp> used to split the substrings into <Array>
     * @return <String> newly constructed from sorted substrings
     */
    public static String splitAndSortsubStrings( String stringToSort, String splitterExp )
    {
        String list[] = stringToSort.split( splitterExp );
        Arrays.sort( list );
        StringBuilder sbuff = new StringBuilder();
        for ( int i = 0; i < list.length; i++ )
        {
            if ( i > 0 )
            {
                sbuff.append( splitterExp );
            }
            sbuff.append( list[i] );
        }

        return sbuff.toString();

    }


    public static String splitAndSortsubStrings( String stringToSort, String splitterExp, String innerSplitter )
    {
        String list[] = stringToSort.split( splitterExp );
        Arrays.sort( list );
        StringBuilder sbuff = new StringBuilder();
        for ( int i = 0; i < list.length; i++ )
        {
            if ( i > 0 )
            {
                sbuff.append( splitterExp );
            }
            sbuff.append( splitAndSortsubStrings( list[i], innerSplitter ) );
        }

        return sbuff.toString();
    }
    
    public static String encodeStringInBase64(String stringToEncode){
    	
    	byte[]   bytesEncoded = Base64.encodeBase64(stringToEncode.getBytes());
    	return new String(bytesEncoded);
    }
    
    public static String compressAndEncodeStringInBase64(String stringToCompressAndEncode) throws IOException{

    	    ByteArrayOutputStream rstBao = new ByteArrayOutputStream();
    	    GZIPOutputStream zos = new GZIPOutputStream(rstBao);
    	    zos.write(stringToCompressAndEncode.getBytes());
    	    //IOUtils.closeQuietly(zos);
    	    zos.close();
    	    
    	    byte[] bytes = rstBao.toByteArray();
    	    return Base64.encodeBase64String(bytes);
    }
    
}
