package com.friday.custody.utils;

import com.friday.custody.common.CustodyContants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.regex.Pattern;

@Slf4j
public class BigDecimalUtil {
    /**
     * 校验数字是否符合校验规则
     */
    public static boolean checkNumberFormat(String str, int bit) {
        if (null == str || str.length() == 0) {
            return false;
        }
        //处理负数情况
        String substring = str.substring(0, 1);
        if("-".equals(substring)){
            str = StringUtils.substringAfter(str, "-");
        }
        String bitStr = bit == 0 ? "" : String.valueOf(bit);
        String regExp = "^[\\d]+(\\.(\\d){0," + bitStr + "})?$";
        return Pattern.compile(regExp).matcher(str).matches();
    }


    public static BigDecimal str2BigDecimal(String str, int decimalLength) {
        if (StringUtils.isBlank(str)) {
            return null;
        }
        //.98 国君
        if (str.startsWith(".")) {
            str = "0" + str;
        }
        str = str.replace("%", "").replace(" ", "").replace(",","");
        //会有科学计数法
        if (str.contains("E")) {
            return new BigDecimal(new BigDecimal(str).toPlainString()).setScale(decimalLength, RoundingMode.HALF_DOWN);
        }
        if (checkNumberFormat(str, 0)) {
            return new BigDecimal(str).setScale(decimalLength, RoundingMode.HALF_DOWN);
        }
        log.error("str2BigDecimal出错,the str is{}", str);
        return null;
    }
    public static BigDecimal str2BigDecimal(String str) {
        return str2BigDecimal(str, 2);
    }

    public static BigDecimal obj2BigDecimal(Object val, int decimalLength) {
        if (Objects.isNull(val)) {
            return null;
        }
        String str = val.toString();
        if (StringUtils.isBlank(str)) {
            return null;
        }
        //.98 国君
        if (str.startsWith(".")) {
            str = "0" + str;
        }
        str = str.replace("%", "").replace(" ", "").replace(",", "");
        //会有科学计数法
        if (str.contains("E")) {
            return new BigDecimal(new BigDecimal(str).toPlainString()).setScale(decimalLength, RoundingMode.HALF_DOWN);
        }
        if (checkNumberFormat(str.replace("-","").replace("+",""), 0)) {
            return new BigDecimal(str).setScale(decimalLength, RoundingMode.HALF_DOWN);
        }
        log.error("obj2BigDecimal出错,the str is{}", str);
        return null;
    }
    public static BigDecimal obj2BigDecimal(Object str) {
        BigDecimal bigDecimal = obj2BigDecimal(str, 2);
        if(bigDecimal==null){
            bigDecimal= new BigDecimal(0);
        }
        return bigDecimal;
    }

    /**
     * 结果为空则转0
     * @param str
     * @return
     */
    public static BigDecimal obj2BigDecimalTrunToZero(Object str, int decimalLength) {
        BigDecimal bigDecimal = obj2BigDecimal(str, decimalLength);
        if (Objects.isNull(bigDecimal)) {
            return new BigDecimal(0).setScale(decimalLength);
        }
        return bigDecimal;
    }
    /**
     * 结果为空则转0
     * @param str
     * @return
     */
    public static BigDecimal obj2BigDecimalTrunToZero(Object str) {
        BigDecimal bigDecimal = obj2BigDecimal(str, 2);
        if (Objects.isNull(bigDecimal)) {
            return new BigDecimal("0.00");
        }
        return bigDecimal;
    }
    public static void main(String[] args) {


        String str = ".98";
        System.out.println("=====================");
        System.out.println("str = " + obj2BigDecimal(".98",2));

    }

    public static BigDecimal str2BigDecimalDefaultZero(String str) {
        BigDecimal bigDecimal = str2BigDecimal(str, 2);
        if(bigDecimal==null){
            bigDecimal= new BigDecimal(0);
        }
        return bigDecimal;
    }
    public static BigDecimal str2BigDecimalDefaultZero(String str, int decimalLength) {
        BigDecimal bigDecimal = str2BigDecimal(str, decimalLength);
        if(bigDecimal==null){
            bigDecimal= new BigDecimal(0);
        }
        return bigDecimal;
    }
    public static BigDecimal getShareRatio(BigDecimal shares, BigDecimal lastHoldShares) {
        if (shares == null || BigDecimal.ZERO.equals(shares) || lastHoldShares == null || BigDecimal.ZERO.equals(lastHoldShares)) {
            return null;
        }
        if (BigDecimalUtil.obj2BigDecimal("0").equals(shares) || BigDecimalUtil.obj2BigDecimal("0").equals(lastHoldShares)){
            return null;
        }
        return lastHoldShares.divide(shares, 6, BigDecimal.ROUND_HALF_UP).multiply(CustodyContants.HUNDRED).setScale(4,BigDecimal.ROUND_HALF_UP);
    }
}
