package com.securitycontrol.common.core.utils.aes;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author HeiZi
 */
public class StringHelper {

	private static final String HEX_STRING = "0123456789ABCDEF";

	public static String replaceAll(String str, String oldStr, String newStr) {
		return str.replaceAll(oldStr, newStr);
	}

	public static boolean contains(String s1, String s2) {
		if (isEmpty(s1)) {
			return false;
		}
		return s1.contains(s2);

	}

	/**
	 * 集合转数组
	 *
	 * @param list
	 * @param separator
	 * @return
	 */
	public static String listToString(List<String> list, char separator) {
		return StringUtils.join(list.toArray(), separator);
	}

	public static void writeText(String args) {
		File fp = new File("F:\\1.txt");
		PrintWriter pfp;
		try {
			pfp = new PrintWriter(fp);
			pfp.print(args);
			pfp.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * blob中文乱码转码
	 * @return
	 */
	public static String blobToStrings(Blob b) {
		try {
			if(b == null) {
				return null;
			}
			String content = new String(b.getBytes((long) 1, (int) b.length()), "UTF-8");
			return content;
		} catch (SQLException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * blob中文乱码转码
	 *
	 * @param content
	 * @return
	 */
	public static String blobToString(String content) {
		try {
			return new String(content.getBytes("ISO_8859_1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return content;
	}

	/**
	 * unicode转字符串
	 *
	 * @param unicode
	 * @return
	 */
	public static String unicodeToString(String unicode) {
		StringBuffer sb = new StringBuffer();
		String[] hex = unicode.split("\\\\u");
		for (int i = 1; i < hex.length; i++) {
			int index = Integer.parseInt(hex[i], 16);
			sb.append((char) index);
		}
		return sb.toString();
	}

	/**
	 *      * 含有unicode 的字符串转一般字符串      * @param unicodeStr 混有 Unicode 的字符串
	 *      * @return      
	 */
	public static String unicodeStr2String(String unicodeStr) {
		int length = unicodeStr.length();
		int count = 0;
		// 正则匹配条件，可匹配“\\u”1到4位，一般是4位可直接使用 String regex = "\\\\u[a-f0-9A-F]{4}";
		String regex = "\\\\u[a-f0-9A-F]{1,4}";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(unicodeStr);
		StringBuffer sb = new StringBuffer();

		while (matcher.find()) {
			String oldChar = matcher.group();
			String newChar = unicode2String(oldChar);
			int index = matcher.start();
			sb.append(unicodeStr.substring(count, index));
			sb.append(newChar);
			count = index + oldChar.length();
		}
		sb.append(unicodeStr.substring(count, length));
		return sb.toString();
	}

	/**
	 * unicode 转字符串
	 * 全为 Unicode 的字符串
	 * @param unicode
	 * @return
	 */
	public static String unicode2String(String unicode) {
		StringBuffer string = new StringBuffer();
		String[] hex = unicode.split("\\\\u");

		for (int i = 1; i < hex.length; i++) {
			// 转换出每一个代码点
			int data = Integer.parseInt(hex[i], 16);
			// 追加成string
			string.append((char) data);
		}

		return string.toString();
	}

	/**
	 * 将字符串编码成16进制数字,适用于所有字符（包括中文）
	 */
	public static String encode(String str) {
		// 根据默认编码获取字节数组
		byte[] bytes = str.getBytes();
		StringBuilder sb = new StringBuilder(bytes.length * 2);
		// 将字节数组中每个字节拆解成2位16进制整数
		for (int i = 0; i < bytes.length; i++) {
			sb.append(HEX_STRING.charAt((bytes[i] & 0xf0) >> 4));
			sb.append(HEX_STRING.charAt((bytes[i] & 0x0f) >> 0));
		}
		return sb.toString();
	}

	public static String decode(String bytes) {
		int num=2;
		ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / num);
		// 将每2位16进制整数组装成一个字节
		for (int i = 0; i < bytes.length(); i += num){
			baos.write((HEX_STRING.indexOf(bytes.charAt(i)) << 4 | HEX_STRING.indexOf(bytes.charAt(i + 1))));
		}

		return new String(baos.toByteArray());
	}

	public static String fillPrefixZero(int v, int len) {
		String vStr = v + "";
		StringBuilder tst=new StringBuilder("0");
		while (vStr.length() < len) {
			vStr =tst.append(vStr).toString();
		}
		return vStr;
	}

	public static boolean isEmpty(String str) {
		return str == null || "".equals(str.trim());
	}

	/**
	 * 判断字符串 不为空
	 */
	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

}
