package com.mx.cneeds.common.converter;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.xml.bind.DatatypeConverter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName RequestParamterConverter
 * @Author zzrdark
 * @Date 2020-03-04 17:54
 * @Description TODO
 **/
public class RequestParamterConverter {

    public static String authorizationConverter(String username, String password){
        StringBuilder sb = new StringBuilder();
        sb.append(username+":");
        sb.append(password);

        String encoding = DatatypeConverter.printBase64Binary(sb.toString().getBytes());

        sb = new StringBuilder();
        sb.append("Basic ");
        sb.append(encoding);
        return sb.toString();
    }


    /**
     * 下划线命名转驼峰命名
     * @param underscore
     * @return
     */
    public static String underscoreToCamelCase(String underscore){
        String[] ss = underscore.split("_");
        if(ss.length ==1){
            return underscore;
        }

        StringBuffer sb = new StringBuffer();
        sb.append(ss[0]);
        for (int i = 1; i < ss.length; i++) {
            sb.append(upperFirstCase(ss[i]));
        }

        return sb.toString();
    }

    /**
     * 驼峰 转下划线
     * @param camelCase
     * @return
     */
    public static String toLine(String camelCase){
        Pattern humpPattern = Pattern.compile("[A-Z]");
        Matcher matcher = humpPattern.matcher(camelCase);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()){
            matcher.appendReplacement(sb, "_"+matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }


    /**
     * 首字母 转小写
     * @param str
     * @return
     */
    private static String lowerFirstCase(String str) {
        char[] chars = str.toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }

    /**
     * 首字母 转大写
     * @param str
     * @return
     */
    private static String upperFirstCase(String str) {
        char[] chars = str.toCharArray();
        chars[0] -= 32;
        return String.valueOf(chars);
    }


    public static Gson getGson(){
        Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
        return gson;
    }

    public static void main(String[] args) {
//        System.out.println(authorizationConverter("clientapp", "112233"));
        System.out.println(toLine("userName"));
    }


}
