package org.springblade.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: Silent
 * @Description
 * @Date: Created in 15:58 2021/11/4
 * @Modified By:
 */
public interface BeanPropertyUtil<T, R> extends Function<T, R>, Serializable {

	/**
	 * 下划线转驼峰法
	 * @param line 源字符串
	 * @param smallCamel 大小驼峰,是否为小驼峰
	 * @return 转换后的字符串
	 */
	static String underlineToCamel(String line,boolean smallCamel){
		if(line==null||"".equals(line)){
			return "";
		}
		StringBuffer sb=new StringBuffer();
		Pattern pattern=Pattern.compile("([A-Za-z\\d]+)(_)?");
		Matcher matcher=pattern.matcher(line);
		while(matcher.find()){
			String word=matcher.group();
			sb.append(smallCamel&&matcher.start()==0?Character.toLowerCase(word.charAt(0)):Character.toUpperCase(word.charAt(0)));
			int index=word.lastIndexOf('_');
			if(index>0){
				sb.append(word.substring(1, index).toLowerCase());
			}else{
				sb.append(word.substring(1).toLowerCase());
			}
		}
		return sb.toString();
	}
	/**
	 * 驼峰法转下划线
	 * @param line 源字符串
	 * @return 转换后的字符串
	 */
	static String camelToUnderline(String line){
		if(line==null||"".equals(line)){
			return "";
		}
		line=String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
		StringBuffer sb=new StringBuffer();
		Pattern pattern=Pattern.compile("[A-Z]([a-z\\d]+)?");
		Matcher matcher=pattern.matcher(line);
		while(matcher.find()){
			String word=matcher.group();
			sb.append(word.toUpperCase());
			sb.append(matcher.end()==line.length()?"":"_");
		}
		return sb.toString();
	}

	/**
	 * 获取字段名并转化为下划线
	 * @param func
	 * @param <T>
	 * @param <R>
	 * @return
	 */
	static <T,R> String getFieldNameToUnder(BeanPropertyUtil<T, R> func) {
		return camelToUnderline(getFieldName(func)).toLowerCase();
	}

	/**
	 * 获取字段
	 * @param func
	 * @param <T>
	 * @return
	 */
	static <T,R> String getFieldName(BeanPropertyUtil<T, R> func) {
		try {
			// 通过获取对象方法，判断是否存在该方法
			Method method = func.getClass().getDeclaredMethod("writeReplace");
			method.setAccessible(Boolean.TRUE);
			// 利用jdk的SerializedLambda 解析方法引用
			SerializedLambda serializedLambda = (SerializedLambda) method.invoke(func);
			String getter = serializedLambda.getImplMethodName();
			return resolveFieldName(getter);
		} catch (ReflectiveOperationException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 解析字段名
	 * @param getMethodName
	 * @return
	 */
	static String resolveFieldName(String getMethodName) {
		if (getMethodName.startsWith("get")) {
			getMethodName = getMethodName.substring(3);
		} else if (getMethodName.startsWith("is")) {
			getMethodName = getMethodName.substring(2);
		}
		// 小写第一个字母
		return firstToLowerCase(getMethodName);
	}

	/**
	 * 小写转化
	 * @param param
	 * @return
	 */
	static String firstToLowerCase(String param) {
		if (StringUtils.isBlank(param)) {
			return "";
		}
		return param.substring(0, 1).toLowerCase() + param.substring(1);
	}
}
