package cn.feast.coding.tools;

import java.util.HashMap;
import java.util.Map;

public class StringUtils {
    /**
     * 返回一个字符串首字母大写
     *
     * @param str
     * @return
     */
    public static String capFirst(String str) {
        str = str.substring(0, 1).toUpperCase() + str.substring(1);
        return str;
    }

    /**
     * 返回一个字符串首字母小写
     *
     * @param str
     * @return
     */
    public static String uncapFirst(String str) {
        str = str.substring(0, 1).toLowerCase() + str.substring(1);
        return str;
    }

    /**
     * 替换字符串中占位符（${}）的内容，模拟el表达式的字符串替换
     * @param openToken
     * @param closeToken
     * @param text
     * @param map
     * @return
     */
    public static String parse(String openToken, String closeToken, String text, Map<String, Object> map) {
        if (map == null || map.size() <= 0) {
            return text;
        }
        if (text == null || text.isEmpty()) {
            return "";
        }
        char[] src = text.toCharArray();
        int offset = 0;
        // search open token
        int start = text.indexOf(openToken, offset);
        if (start == -1) {
            return text;
        }
        final StringBuilder builder = new StringBuilder();
        StringBuilder expression = null;
        while (start > -1) {
            if (start > 0 && src[start - 1] == '\\') {
                // this open token is escaped. remove the backslash and continue.
                builder.append(src, offset, start - offset - 1).append(openToken);
                offset = start + openToken.length();
            } else {
                // found open token. let's search close token.
                if (expression == null) {
                    expression = new StringBuilder();
                } else {
                    expression.setLength(0);
                }
                builder.append(src, offset, start - offset);
                offset = start + openToken.length();
                int end = text.indexOf(closeToken, offset);
                while (end > -1) {
                    if (end > offset && src[end - 1] == '\\') {
                        // this close token is escaped. remove the backslash and continue.
                        expression.append(src, offset, end - offset - 1).append(closeToken);
                        offset = end + closeToken.length();
                        end = text.indexOf(closeToken, offset);
                    } else {
                        expression.append(src, offset, end - offset);
                        // offset = end + closeToken.length()
                        break;
                    }
                }
                if (end == -1) {
                    // close token was not found.
                    builder.append(src, start, src.length - start);
                    offset = src.length;
                } else {
                    String value = map.get(expression.toString()) == null ? "" : (String) map.get(expression.toString());
                    builder.append(value);
                    offset = end + closeToken.length();
                }
            }
            start = text.indexOf(openToken, offset);
        }
        if (offset < src.length) {
            builder.append(src, offset, src.length - offset);
        }
        return builder.toString();
    }

    public static String replacePlaceholder(String text, Map<String, Object> map){
        return parse("${", "}", text, map);
    }

    public static void main(String[] args) {
        String text = "hello,${name},你好${pass},${name}";
        Map<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("pass", "123");
        String str = replacePlaceholder(text, map);
        System.out.println(str);
    }
}
