package cn.net.connor.std.processedit.util;

import java.util.ArrayList;
import java.util.List;

import cn.net.connor.std.aifrcp.util.KUtil;

public class KChineseSplitter {

	public static List<String> getDivideVals(String propVal, int maxLen) {
		return getDivideVals(propVal, maxLen, "\n");
		// List<String> res = new ArrayList<>();
		// if(KUtil.isEmpty(propVal)||maxLen<=0) {
		// res.add(propVal);
		// return res;
		// }
		// String[] texts = propVal.split("\n");
		// for (int j = 0; j < texts.length; j++) {
		// String text = texts[j];
		// if (maxLen > 0) {
		// List<String> splitRes = splitStrFullCharacter(text, maxLen);
		// res.addAll(splitRes);
		// } else {
		// res.add(text);
		// }
		// }
		// return res;
	}

	public static List<String> getDivideVals(String propVal, int maxLen, String splitFlag) {
		List<String> res = new ArrayList<>();
		if (KUtil.isEmpty(propVal) || maxLen <= 0) {
			if (splitFlag != null && !"".equals(splitFlag) && !KUtil.isEmpty(propVal)) {
				String[] texts = propVal.split(splitFlag);
				for (int j = 0; j < texts.length; j++) {
					res.add(texts[j]);
				}
			} else {
				res.add(propVal);
			}
			return res;
		}
		String[] texts;
		if (splitFlag == null || "".equals(splitFlag)) {
			texts = new String[] { propVal };
		} else {
			texts = propVal.split(splitFlag);
		}
		for (int j = 0; j < texts.length; j++) {
			String text = texts[j];
			if (maxLen > 0) {
				List<String> splitRes = splitStrFullCharacter(text, maxLen);
				res.addAll(splitRes);
			} else {
				res.add(text);
			}
		}
		return res;
	}

	public static List<String> splitStrFullCharacter(String str, int charNum) {
		List<String> res = new ArrayList<>();
		int index = getSplitIndex(str, charNum);
		while (index > 0) {
			res.add(str.substring(0, index));
			str = str.substring(index);
			index = getSplitIndex(str, charNum);
		}
		// if (!"".equals(str)) {
		res.add(str);
		// }
		return res;
	}

	// public static double getFullCharacterLength(String str) {
	// char[] chars = str.toCharArray();
	// double counter = 0;
	// for (int i = 0; i < chars.length; i++) {
	// if (isFullCharacter(chars[i])) {
	// counter += 1;
	// } else {
	// counter += 0.5;
	// }
	// }
	// return counter;
	// }

	public static int getSplitIndex(String str, int charNum) {
		char[] chars = str.toCharArray();
		double counter = 0;
		for (int i = 0; i < chars.length; i++) {
			if (isFullCharacter(chars[i])) {
				counter += 1;
			} else {
				counter += 0.5;
			}
			if (counter == charNum) {
				return i + 1;
			} else if (counter > charNum) {
				return i;
			}
		}
		return -1;
	}

	// 根据Unicode编码完美的判断中文汉字和符号
	public static boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
			return true;
		}
		return false;
	}

	public static boolean isFullCharacter(char c) {
		if (isChinese(c)) {
			return true;
		}
		String temp = String.valueOf(c);
		if (temp.matches("[\u4e00-\u9fa5]")) {
			return true;
		}
		int charValue = (int) c;
		// System.out.println(charValue);
		return (charValue >= 65281 && charValue <= 65374) || (charValue >= 12288 && charValue <= 12290 || charValue == 9474);
	}
}