package com.pictc.utils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegexUtils {
	
	private static final Set<String> symbols=SetUtils.of("\\","^","$","*","+","?","{","}","[","]",".","|","-","\\b","\\B"
			,"\\c","\\d","\\D","\\f","\\n","\\r","\\s","\\S","\\t","\\v","\\w","\\W","\\x","\\u");
	
	private static final String prefix="${";
	
	private static final String suffixed="}";
	
	private static final Pattern placeholder_regex=buildPlaceholderPattern(prefix, suffixed);
	
	private RegexUtils() {
		super();
	}
	
	public static boolean has(String regex,String str) {
		return has(Pattern.compile(regex), str);
	}
	
	/**
	 * 判断字符中是否包含占位符
	 * */
	public static boolean has(Pattern pattern,String str) {
		return pattern.matcher(str).find();
	}
	
	public static boolean has(String str) {
		return placeholder_regex.matcher(str).find();
	}
	
	public static List<String> findPlaceholder(String str) {
		return findPlaceholder(str,placeholder_regex,2,1);
	}
	
	public static List<String> findPlaceholder(String str,String regex,int prefixLeng,int endLeng) {
		return findPlaceholder(str,Pattern.compile(regex),prefixLeng,endLeng);
	}
	
	/**
	 * 查找字符串中的占位符中的字符
	 * */
	public static List<String> findPlaceholder(String str,Pattern pattern,int prefixLeng,int endLeng) {
		Matcher m=pattern.matcher(str);
		List<String> result=null;
		while (m.find()) {
			if(result==null) result=CollectionUtils.newArrayList();
			result.add(m.group().substring(prefixLeng, m.group().length()-endLeng));
		}
		return result;
	}
	
	public static String replacePlaceholder(String str,String[] replaceStr) {
		return replacePlaceholder(placeholder_regex, str, replaceStr);
	}
	
	
	public static String replacePlaceholder(String str,Map<String,String> values) {
		List<String> placeholders = findPlaceholder(str);
		List<String> replaceValues = CollectionUtils.newArrayList();
		for (String key : placeholders) {
			replaceValues.add(values.get(key));
		}
		String[] _vals = new String[replaceValues.size()];
		replaceValues.toArray(_vals);
		return replacePlaceholder(placeholder_regex, str,_vals);
	}
	
	public static String replacePlaceholder(Pattern regex,String str,String[] replaceStr) {
		Matcher m=regex.matcher(str);
		StringBuilder result=new StringBuilder();
		int index=0;int before=0;
		while (m.find()) {
			result.append(str.subSequence(before, m.start()));
			before=m.end();
			if(index<replaceStr.length) {
				String _value = replaceStr[index];
				if(_value!=null) {
					result.append(_value);
				}
			}
			index++;
		}
		if(str.length()!=before) result.append(str.subSequence(before, str.length()));
		return result.toString();
	}
	
	/**
	 * 转义字符中的正则元符
	 * */
	public static String decodeRegex(String str) {
		for (String key : symbols) {
			if(str.contains(key)) {
				str=str.replace(key,"\\"+key);
			}
		}
		return str;
	}
	
	/**
	 * 创建占位符的正则表达式
	 * */
	public static Pattern buildPlaceholderPattern(String prefix,String suffixed) {
		prefix=decodeRegex(prefix);
		suffixed=decodeRegex(suffixed);
		return Pattern.compile(String.format("%s[^%s]*%s", prefix,suffixed,suffixed));
	}
	

}
