package com.brilliance.mda.runtime.mda.util;

import java.math.BigDecimal;

public class Decimals {

    public static final BigDecimal ZERO_SCALE0 = BigDecimal.ZERO;
    public static final BigDecimal ZERO_SCALE1 = BigDecimal.valueOf(0l,1);
    public static final BigDecimal ZERO_SCALE2 = BigDecimal.valueOf(0l,2);
    public static final BigDecimal ZERO_SCALE3 = BigDecimal.valueOf(0l,3);
    public static final BigDecimal ZERO_SCALE4 = BigDecimal.valueOf(0l,4);
    public static final BigDecimal ZERO_SCALE5 = BigDecimal.valueOf(0l,5);
    public static final BigDecimal ZERO_SCALE6 = BigDecimal.valueOf(0l,6);
    public static final BigDecimal ZERO_SCALE7 = BigDecimal.valueOf(0l,7);
    public static final BigDecimal ZERO_SCALE8 = BigDecimal.valueOf(0l,8);
    public static final BigDecimal ZERO_SCALE9 = BigDecimal.valueOf(0l,9);
    public static final BigDecimal ZERO_SCALE10 = BigDecimal.valueOf(0l,10);

    public static BigDecimal remainderOne(BigDecimal d){
        return d.remainder(BigDecimal.ONE);
    }

    public static boolean gt(BigDecimal d1,BigDecimal d2){
        return d1.compareTo(d2)>0;
    }

    public static Boolean chkNum(Object o) {
        return o instanceof Integer || o instanceof Float || o instanceof Double || o instanceof Short || o instanceof Long || o instanceof BigDecimal;
    }
    public static BigDecimal toBigDecimal(Object o){
        if(o instanceof Integer)
        {
            return BigDecimal.valueOf((Integer)o);
        }
        if (o instanceof Float){
            return BigDecimal.valueOf((Float)o);
        }
        if (o instanceof Double){
            return BigDecimal.valueOf((Double)o);
        }
        if (o instanceof Short){
            return BigDecimal.valueOf((Short)o);
        }
        if (o instanceof Long){
            return BigDecimal.valueOf((Long)o);
        }
        if(o instanceof BigDecimal){
            return (BigDecimal) o;
        }
        return new BigDecimal(o.toString());

    }

    public static BigDecimal add(Object m, Object n) {
        if (m == null){
            m = 0;
        }
        if (n == null){
            n = 0;
        }
        if (!chkNum(m) || !chkNum(n)) {
            throw new IllegalArgumentException("参数必须都是数字");
        }
        BigDecimal addend1 = chkNum(m) ? toBigDecimal(m) : new BigDecimal(m.toString());
        BigDecimal addend2 = chkNum(n) ? toBigDecimal(n ): new BigDecimal(n.toString());
        return addend1.add(addend2);
    }

    public static BigDecimal sub(Object m, Object n) {
        if (m == null){
            m = 0;
        }
        if (n == null){
            n = 0;
        }
        if (!chkNum(m) || !chkNum(n)) {
            throw new IllegalArgumentException("参数必须都是数字");
        }
        BigDecimal subtrahend1 = chkNum(m) ? toBigDecimal(m) : new BigDecimal(m.toString());
        BigDecimal subtrahend2 = chkNum(n) ? toBigDecimal(n) : new BigDecimal(n.toString());
        return subtrahend1.subtract(subtrahend2);
    }

    public static BigDecimal mul(Object m, Object n) {
        if (m == null){
            m = 0;
        }
        if (n == null){
            n = 0;
        }
        if (!chkNum(m) || !chkNum(n)) {
            throw new IllegalArgumentException("参数必须都是数字");
        }
        BigDecimal multiplicand = chkNum(m) ? toBigDecimal(m) : new BigDecimal(m.toString());
        BigDecimal multiplier = chkNum(n) ? toBigDecimal(n) : new BigDecimal(n.toString());
        return multiplicand.multiply(multiplier);
    }

    //BigDecimal做除法默认保留10位
    public static final int BigDecimal_Divide_Default_Length = 10;

    public static BigDecimal div(Object m, Object n) {
        if (m == null){
            m = 0;
        }
        if (!chkNum(m) || !chkNum(n)) {
            throw new IllegalArgumentException("参数必须都是数字");
        }
        if (chkNum(n)) {
            if (n == BigDecimal.ZERO) {
                throw new IllegalArgumentException("除法不能为零");
            }
        } else {
            if (Double.valueOf(n.toString()) == 0) {
                throw new IllegalArgumentException("除法不能为零");
            }
        }
        BigDecimal dividend = chkNum(m) ? toBigDecimal(m) : new BigDecimal(m.toString());
        BigDecimal divisor = chkNum(n) ? toBigDecimal(n) : new BigDecimal(n.toString());
        return dividend.divide(divisor, BigDecimal_Divide_Default_Length, BigDecimal.ROUND_HALF_UP);
    }

}
