package cn.util.string;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import cn.constant.CharsetKit;
import cn.util.core.IsKit;
import cn.util.core.TypeKit;

/**
 * 命名转换工具类
 */
public class NamingKit 
{
	/**
	 * 百分号编码工具方法
	 * @param s 需要百分号编码的字符串
	 * @return 百分号编码后的字符串
	 */
	public static String percentEncode(String s) throws UnsupportedEncodingException
	{
		String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
		return encode.replaceAll("\\+", "%20");
	}
	
	/**将List中的key中大写驼峰表示法转为下划线为表示法*/
	public static List<Map<String,Object>> camelToUnderline(List<Map<String,Object>> list)
	{
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();

		if(IsKit.isNullOrEmpty(list)) {
			return resultList;
		}
		int size = list.size();
		for(int i=0;i<size;i++){
			Map<String,Object> resultMap = camelToUnderline(list.get(i));
			if (IsKit.isNotNullOrEmpty(resultMap)) {
				resultList.add(resultMap);
			}	
		}
		return resultList;
	}

	/**将map中的key中大写驼峰表示法转为下划线为表示法*/
	public static Map<String, Object> camelToUnderline(Map<String,Object> map)
	{
		Map<String, Object> resultMap = new HashMap<>();
		if (IsKit.isNullOrEmpty(map)) {
			return resultMap;
		}		
		for(Object key : map.keySet()){
			resultMap.put(camel2Underline(TypeKit.objToStr(key)),map.get(key));
		}
		return resultMap;
	}

	/**
	 * 驼峰转下划线 aB->a_b
	 */
	public static String camel2Underline(String str){
		Pattern humpPattern = Pattern.compile("[A-Z]");
		Matcher matcher = humpPattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while(matcher.find()){
			matcher.appendReplacement(sb, "_"+matcher.group(0).toLowerCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
	public static String camelToUnderline(String param) 
	{
		if (IsKit.isNullOrEmpty(param)) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		int len = param.length();
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (Character.isUpperCase(c)) {
				if(sb.length() > 0 )
					sb.append(CharsetKit.SYMBOL_UNDERSCORE);//_
				sb.append(Character.toLowerCase(c));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	
	/**将List中的key中下划线转驼峰法*/
	public static List<Map<String,Object>> underlineToCaml(List<Map<String,Object>> list)
	{
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		if(IsKit.isNullOrEmpty(list)) {
			return resultList;
		}
		int size = list.size();
		for(int i=0;i<size;i++)
		{
			Map<String,Object> resultMap = underlineToCaml(list.get(i));
			if (IsKit.isNotNullOrEmpty(resultMap)) {
				resultList.add(resultMap);
			}	
		}
		return resultList;
	}
	/**将map中的key中划线表示法转为大写驼峰表示法*/
	public static Map<String, Object> underlineToCaml(Map<String, Object> map)
	{
		Map<String,Object> resultMap = new HashMap<>();
		if (IsKit.isNullOrEmpty(map)) {
			return resultMap;
		}		
		for(Object key : map.keySet()){
			resultMap.put(underlineToCamel(TypeKit.objToStr(key)),map.get(key));
		}
		return resultMap;
	}
	
	//下划线转大驼峰a_b->aB
	public static String underlineToCamel(String param)
	{
		if (IsKit.isNullOrEmpty(param)) {
			return null;
		}
		StringBuilder sb = new StringBuilder();
		int len = param.length();
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (c==CharsetKit.C_UNDERLINE) {//_
				if(++i<len){
					sb.append(Character.toUpperCase(param.charAt(i)));
				}
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}
	
	
}
