package id.iptek.utms.agent.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.id.random.SessionIdGenerator;

/**
 *
 * @author Jaka Ramdani
 */
public final class StringUtil {

    private StringUtil() {
    }

    public static String decodeHtml(String strValue) {
        String strResult = "";
        if (strValue != null) {
            String strInfo = strValue;
            StringBuffer sbResult = null;
            try {
                sbResult = new StringBuffer(strInfo);

                int intStart = 0;
                int intEnd = 0;
                while (true) {
                    if (strInfo.indexOf("&lt;") != -1) {
                        intStart = strInfo.indexOf("&lt;");
                        intEnd = intStart + "&lt;".length();

                        sbResult.replace(intStart, intEnd, "<");
                        strInfo = sbResult.toString();
                        continue;
                    }
                    if (strInfo.indexOf("&gt;") != -1) {
                        intStart = strInfo.indexOf("&gt;");
                        intEnd = intStart + "&gt;".length();

                        sbResult.replace(intStart, intEnd, ">");
                        strInfo = sbResult.toString();
                        continue;
                    }
                    if (strInfo.indexOf("&amp;") != -1) {
                        intStart = strInfo.indexOf("&amp;");
                        intEnd = intStart + "&amp;".length();

                        sbResult.replace(intStart, intEnd, "&");
                        strInfo = sbResult.toString();
                        continue;
                    }
                    if (strInfo.indexOf("&quot;") != -1) {
                        intStart = strInfo.indexOf("&quot;");
                        intEnd = intStart + "&quot;".length();

                        sbResult.replace(intStart, intEnd, "\"");
                        strInfo = sbResult.toString();
                        continue;
                    }
                    if (strInfo.indexOf("&nbsp;") == -1) {
                        break;
                    }
                    intStart = strInfo.indexOf("&nbsp;");
                    intEnd = intStart + "&nbsp;".length();

                    sbResult.replace(intStart, intEnd, " ");
                    strInfo = sbResult.toString();
                }

                strResult = sbResult.toString();
            } catch (Exception e) {
                strResult = "";
            } finally {
                sbResult = null;
            }
        }
        return strResult;
    }

    public StringBuilder collect(String msg) {
        StringBuilder sb = new StringBuilder();
        sb.append("<HTML>\n");
        sb.append("<HEAD>\n");
        sb.append("<TITLE>\n");
        sb.append("</TITLE>\n");
        sb.append("</HEAD>\n");
        sb.append("<BODY background=#ffffff>\n");
        for (int i = 0; i < msg.length(); i++) {
            char currentChar = msg.charAt(i);
            if ((currentChar == '\n') || (currentChar == '\n')) {
                sb.append("<BR>");
            } else if (currentChar == ' ') {
                sb.append("&nbsp;");
            } else {
                sb.append(currentChar);
            }
        }
        sb.append("</BODY>\n");
        sb.append("</HTML>\n");
        return sb;
    }

    public static String getMD5Hash(String string) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(string.getBytes());
            byte[] digest = md5.digest();            
            string = Base64.encodeBase64String(digest);
        } catch (NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        }

        return string;
    }

    private static String byteArrToHexString(byte[] bArr) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bArr.length; i++) {
            int unsigned = bArr[i] & 0xff;
            if (unsigned < 0x10) {
                sb.append("0");
            }
            sb.append(Integer.toHexString((unsigned)));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        SessionIdGenerator gidGenerator = new SessionIdGenerator();
        String gid = DigestUtils.md5Hex(gidGenerator.nextStringIdentifier());
        System.err.println(">> gid: " + gid);
    }
}
