package org.well.well.core.util;

import org.apache.commons.lang3.StringEscapeUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

public class StringUtils {
    protected StringUtils() {
    }


    public static final String SYS_USER = "current_user";
    static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
            '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    public static boolean isEmpty(String text) {
        return org.apache.commons.lang3.StringUtils.isEmpty(text);
    }

    public static boolean isBlank(String text) {
        return org.apache.commons.lang3.StringUtils.isBlank(text);
    }

    public static boolean isNotBlank(String text) {
        return org.apache.commons.lang3.StringUtils.isNotBlank(text);
    }

    public static String capitalize(String text) {
        return org.apache.commons.lang3.StringUtils.capitalize(text);
    }

    public static String substring(String text, int offset, int limit) {
        return org.apache.commons.lang3.StringUtils.substring(text, offset,
                limit);
    }

    public static String substringBefore(String text, String token) {
        return org.apache.commons.lang3.StringUtils
                .substringBefore(text, token);
    }

    public static String substringAfter(String text, String token) {
        return org.apache.commons.lang3.StringUtils.substringAfter(text, token);
    }

    public static String[] splitByWholeSeparator(String text, String separator) {
        return org.apache.commons.lang3.StringUtils.splitByWholeSeparator(text,
                separator);
    }

    public static String join(List list, String separator) {
        return org.apache.commons.lang3.StringUtils.join(list, separator);
    }

    public static String escapeHtml(String text) {
        return StringEscapeUtils.escapeHtml4(text);
    }

    public static String unescapeHtml(String text) {
        return StringEscapeUtils.unescapeHtml4(text);
    }

    public static String escapeXml(String text) {
        return StringEscapeUtils.escapeXml11(text);
    }

    public static String unescapeXml(String text) {
        return StringEscapeUtils.unescapeXml(text);
    }

    public static String trim(String text) {
        if (text == null) {
            return null;
        }

        text = text.replace("" + ((char) 160), " ");

        text = org.apache.commons.lang3.StringUtils.trim(text);
        text = org.apache.commons.lang3.StringUtils.strip(text, "　");

        return text;
    }

    public static String getMD5(String message)
    {
        try
        {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] b = md.digest(message.getBytes());

            return byteToHexString(b);
            //return byteToString(b);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }return null;
    }

    private static String byteToHexString(byte[] tmp)
    {
        char[] str = new char[32];

        int k = 0;
        for (int i = 0; i < 16; i++)
        {
            byte byte0 = tmp[i];
            str[(k++)] = hexDigits[(byte0 >>> 4 & 0xF)];

            str[(k++)] = hexDigits[(byte0 & 0xF)];
        }
        String s = new String(str);
        return s;
    }

    public static String JM(String inStr){
        char[] a=inStr.toCharArray();
        for(int i=0;i<a.length;i++){
            a[i]=(char)(a[i]^'t');
        }
        String password=new String(a);
        return password;
    }




    public static <E> E convers(Object[] objects, int i, Class<E> clasz) {
        if (objects != null && objects.length > i && i > -1) {
            Object s = objects[i];
            if (clasz == Float.class) {
                return (E) new Float(Float.parseFloat((String) s));
            }
            if (clasz == Integer.class) {
                return (E) new Integer(Integer.parseInt((String) s));
            }
            return (E) s;
        }
        return null;
    }
}
