package com.foreverwin.mesnac.common.util;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil
{
	public static final String SPLITOR_COMMON = "_#,#_";
	public static final String NUMBER_CHARS = "0123456789";
	public static final String UPPER_ALPHA_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String UPPER_ALPHA_NUMBER_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String ALPHA_NUMBER_CHARS = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		
	private static final String[] CHINESE_MONEY_NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};  
	private static final String[] CHINESE_MONEY_INTEGER = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟"};  
    private static final String[] CHINESE_MONEY_DECIMAL = {"角", "分", "厘"};

	private static char[] QUID_ALPHA_NUMBER = {
			'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
			'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
			'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
			'U', 'V', 'W', 'X', 'Y', 'Z' };

	private static char[] QUID_ALPHA = {
			'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
			'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
			'U', 'V', 'W', 'X', 'Y', 'Z' };

	public static String createQUID()
	{
		return createAlphaNumericText(32);
	}

	public static String createAlphaNumericText(int length)
	{
		Random random = new Random();
		StringBuilder str_builer = new StringBuilder();
		char c = '\000';

		c = QUID_ALPHA[random.nextInt(QUID_ALPHA.length)];
		str_builer.append(c);

		for (int i = 0; i < length - 1; i++)
		{
			c = QUID_ALPHA_NUMBER[random.nextInt(QUID_ALPHA_NUMBER.length)];
			str_builer.append(c);
		}

		return str_builer.toString();
	}
	public static String trimHandle(String handle) {
		if (handle == null || handle.trim().length() == 0) {
			return null;
		}

		String[] temp = handle.split(",");
		if( temp.length < 2 ) {
			return handle;
		}
		return temp[1];
	}

	public static String trimRevision(String handle) {
		if (handle == null || handle.trim().length() == 0) {
			return null;
		}

		int index = handle.lastIndexOf(",");
		if (index < 0) {
			return null;
		}
		else {
			return handle.substring(index + 1);
		}
	}

	public static String append(String str, String appender, int position)
	{
		if (toString(str).equals("")) 
		{
			return appender;
		}
		else if (toString(appender).equals("")) 
		{
			return str;
		}
		else if (position <= 0) 
		{
			 return appender + str;
		}
		else if (position > str.length()) 
		{
			return str + appender;
		} 
		
		String substr1 = str.substring(0, position);
		String substr2 = str.substring(position, str.length());
		return substr1 + appender + substr2;
	}

	public static int indexOf(String str, String substr)
	{
		if (!toString(str).equals(""))
		{
			return str.indexOf(substr);
		}
		return -1;
	}
	
	public static int lastIndexOf(String str, String substr)
	{
		if (!toString(str).equals(""))
		{
			return str.lastIndexOf(substr);
		}
		return -1; 
	}

	static public boolean isBlank(String str)
	{
		boolean isBlank = false;
		if (str == null) isBlank = true;
		else if (str.trim().equals("")) isBlank = true;
		else if (str.equals("null")) isBlank = true;
		return isBlank;
	}

	static public boolean notBlank(String str)
	{
		return !isBlank(str);
	}
	
	public static boolean isEmpty(String str)
	{
		if (toString(str).equals("")) 
		{
			return true;
		}
		return false;
	}

	public static boolean notEmpty(String str)
	{
		return !isEmpty(str);
	}

	public static String random(int length)
	{	
		return random(length, ALPHA_NUMBER_CHARS);
	}
	
	public static String random(int length, String seed)
	{
		if (length < 1) 
		{
			return "";
		}
		
		Random random = new Random();
		char[] chars = new char[length];
		char[] letters = seed.toCharArray();
		for (int i = 0; i < chars.length; i++) 
		{
			chars[i] = letters[random.nextInt(seed.length())];
		}
		return new String(chars);
	}
	
	public static String space(String str, char spacer, int slotter)
	{
		if (!toString(str).equals("")) 
		{
			if (str.length() >= slotter) 
			{
				return str; 
			}
			else 
			{
				String result = str; 
				slotter = slotter - str.length(); 
				for (int i = 0; i < slotter; i++) 
				{
					result = spacer + result; 
				}
				return result;
			}
		}
		return "";
	}

	public static Map<String, String> convertMap(Map<String, String> map)
	{
		if (map == null) return map;
		Map<String, String> result = new HashMap<>();
		Set<String> keys = map.keySet();
		for (String key : keys) {
			result.put(map.get(key), key);
		}
		return result;
	}

	public static Map<Integer, String> split(String str)
	{
		Map<Integer, String> strs = new HashMap<Integer, String>();
		if (!toString(str).equals(""))
		{
			String[] _strs = str.split(SPLITOR_COMMON);
			for (int i = 0; i < _strs.length; i++) 
			{
				strs.put(i, _strs[i]);
			}
		}
		return strs;
	}
	
	public static String[] split(String str, String splitter)
	{
		if (!toString(str).equals(""))
		{
			return str.split(splitter);
		}	
		return new String[0];
	}
	
	public static int splitLength(String str, String splitter)
	{
		return split(str, splitter).length;
	}
	
	public static String substring(String str, int limit)
	{
		if (!toString(str).equals(""))
		{
			if (str.length() <= limit) 
			{
				return str;
			}
			else 
			{
				return str.substring(0, limit) + "...";
			}
		}
		return "";		
	}
	
	public static String hide(String str, int headLength, int tailLength)
	{
		if (!toString(str).equals(""))
		{
			if ((str.length() < headLength) || (str.length() < tailLength))
			{
				headLength = 1;
				tailLength = 1;
			}
			
			String head = str.substring(0, headLength);
			String tail = str.substring(str.length() - tailLength, str.length());
			return head + "***" + tail;
		}
		return "";		
	}

	public static String filterHtml(String html)
  	{
	    Pattern scriptPattern = Pattern.compile("<[//s]*?script[^>]*?>[//s//S]*?<[//s]*?///[//s]*?script[//s]*?>", 2);
	    Matcher scriptMatcher = scriptPattern.matcher(html);
	    html = scriptMatcher.replaceAll("");
	      
	    Pattern cssPattern = Pattern.compile("<[//s]*?style[^>]*?>[//s//S]*?<[//s]*?///[//s]*?style[//s]*?>", 2);
	    Matcher cssMatcher = cssPattern.matcher(html);
	    html = cssMatcher.replaceAll("");
     
        Pattern htmlPattern1 = Pattern.compile("<[^>]+>", 2);
        Matcher htmlMatcher1 = htmlPattern1.matcher(html);
        html = htmlMatcher1.replaceAll("");
	     
	    Pattern htmlPattern2 = Pattern.compile("<[^>]+", 2);
	    Matcher htmlMatcher2 = htmlPattern2.matcher(html);
	    html = htmlMatcher2.replaceAll("");
  		return html;
  	}
	
	public static String wrapHtml(String str)
	{
		String result = "";
		if (!toString(str).equals(""))
		{
			for (int i = 0; i < str.length(); i++)
			{ 
				char _char = str.charAt(i); 
				result += _char + "<br/>";
		    } 
		}
		return result;
	}
	
    public static String toString(Object obj)
	{
    	if (obj != null) 
    	{
    		return obj.toString().trim();
    	}
    	return "";
	}

	public static String toUpperCase(Object obj)
	{
		return toString(obj).toUpperCase();
	}

	public static String toLowerCase(Object obj)
	{
		return toString(obj).toLowerCase();
	}

	public static Set<String> toSet(String str)
	{
		return toSet(str, ",");
	}

	public static Set<String> toSet(String str, String split)
	{
	    Set<String> strings = new LinkedHashSet<String>();
	    if (isEmpty(str)) return strings;

		String[] elements = str.split(",");
	    for (String element : elements)
	    {
	    	String string = toString(element);
	    	if (!string.equals("")) 
	    	{
	    		strings.add(string);
	    	}
	    }
	    return strings;
	}
	
	public static List<String> toList(String str)
	{
	    List<String> list = new ArrayList<String>();
		String[] elements = str.split(",");
	    for (String element : elements)
	    {
	    	String string = toString(element);
	    	list.add(string);
	    }
	    return list;
	}

	public static String toChineseMoney(String str) 
    {       	
    	String integerStr;
    	String decimalStr;
    	str = toString(str);
    	str = str.replaceAll(",", "");
    	if (str.indexOf(".") > 0) 
    	{      
    		integerStr = str.substring(0, str.indexOf("."));      
    		decimalStr = str.substring(str.indexOf(".") + 1);    
        } 
    	else if (str.indexOf(".") == 0) 
    	{     
    		integerStr = "";     
    		decimalStr = str.substring(1);    
        } 
    	else 
    	{      
    		integerStr = str;      
    		decimalStr = "";    
        }      
    	
    	if (!integerStr.equals("")) 
    	{      
    		integerStr = Long.toString(Long.parseLong(integerStr));      
    		if (integerStr.equals("0")) 
    		{        
    			integerStr = "";      
    	    }   
    	}    
    	
    	if (integerStr.length() > CHINESE_MONEY_INTEGER.length) 
    	{     
    		return str;    
        }    
    	
    	int[] integers = new int[integerStr.length()];    
		for (int i = 0; i < integerStr.length(); i++) 
		{     
			integers[i] = Integer.parseInt(integerStr.substring(i, i + 1));  
		}  
		
		boolean isMust5 = false;
		int length = integerStr.length();   
		if (length > 4) 
		{      
			String subInteger = "";     
			if (length > 8) 
			{        
				subInteger = integerStr.substring(length - 8, length - 4);      
			} 
			else 
			{       
				subInteger = integerStr.substring(0, length - 4);     
			} 
			
			if (Integer.parseInt(subInteger) > 0) 
			{
				isMust5 = true;
			}
		} 
    	
    	int[] decimals = new int[decimalStr.length()];    
		for (int i = 0; i < decimalStr.length(); i++) 
		{     
			decimals[i] = Integer.parseInt(decimalStr.substring(i, i + 1));  
		} 
		
		StringBuffer chineseInteger = new StringBuffer("");    
		length = integers.length;   
		for (int i = 0; i < length; i++)
		{       
			String key = "";      
			if (integers[i] == 0) 
			{        
				if ((length - i) == 13)    
				{
					key = CHINESE_MONEY_INTEGER[4];        
				}     
				else if ((length - i) == 9)     
				{
					key = CHINESE_MONEY_INTEGER[8];     
				}  
				else if ((length - i) == 5 && isMust5)
				{
					key = CHINESE_MONEY_INTEGER[4];        
				}	
				else if ((length - i) == 1) 
				{
					key = CHINESE_MONEY_INTEGER[0];        
				}
					
				if ((length - i) > 1 && integers[i + 1] != 0)    
				{
					key += CHINESE_MONEY_NUMBERS[0];     
				}    
			}      
			
			chineseInteger.append(integers[i] == 0 ? key  : (CHINESE_MONEY_NUMBERS[integers[i]] + CHINESE_MONEY_INTEGER[length - i - 1]));    
		}    
				
		StringBuffer chineseDecimal = new StringBuffer("");    
		for (int i = 0; i < decimals.length; i++) 
		{      
			if (i == 3) 
			{
				break;      
			}        
			
			chineseDecimal.append((decimals[i] == 0) ? "" : (CHINESE_MONEY_NUMBERS[decimals[i]] + CHINESE_MONEY_DECIMAL[i]));   
		}    

    	return chineseInteger.toString() + chineseDecimal.toString(); 
	}

	public static boolean toBoolean(Object value)
	{
		try {
			if (value == null) return false;

			return toBoolean(value.toString());
		}
		catch (Exception localException)
		{
		}

		return false;
	}

	public static boolean toBoolean(String str)
	{
		try {
			if (str == null) return false;
			if (str.equalsIgnoreCase("TRUE")) return true;
			if (str.equalsIgnoreCase("Y")) return true;
			if (str.equalsIgnoreCase("YES")) return true;
			if (Integer.parseInt(str) > 0) return true;
		}
		catch (Exception localException)
		{
		}

		return false;
	}

	
	public static String removeSuffix(String str)
	{
		int position = str.lastIndexOf(".");
		if (position > -1)
		{
			str = str.substring(0, position);
		}
		return str;		
	}

	public static void main(String[] args)
	{
		System.out.println(StringUtil.createQUID());
	}
}