package it.integry.integrywmsnative.core.utility;

import java.math.BigDecimal;
import java.math.RoundingMode;

import it.integry.integrywmsnative.core.CommonConst;

public class UtilityBigDecimal {

    public static boolean greaterThan(BigDecimal input, BigDecimal toCompareWith) {
        if (input == null) return false;
        if (toCompareWith == null) return false;

        return input.compareTo(toCompareWith) > 0;
    }

    public static boolean equalsTo(BigDecimal input, BigDecimal toCompareWith) {
        if (input == null && toCompareWith == null) return true;
        if (input == null) return false;
        if (toCompareWith == null) return false;

        return input.compareTo(toCompareWith) == 0;
    }

    public static boolean lowerThan(BigDecimal input, BigDecimal toCompareWith) {
        if (input == null) return false;
        if (toCompareWith == null) return false;

        return input.compareTo(toCompareWith) < 0;
    }

    public static boolean equalsOrLowerThan(BigDecimal input, BigDecimal toCompareWith) {
        return equalsTo(input, toCompareWith) || lowerThan(input, toCompareWith);
    }

    public static boolean equalsOrGreaterThan(BigDecimal input, BigDecimal toCompareWith) {
        return equalsTo(input, toCompareWith) || greaterThan(input, toCompareWith);
    }

    public static BigDecimal getLowerBetween(BigDecimal input1, BigDecimal input2) {
        if (input1 == null) return input2;
        if (input2 == null) return input1;

        return lowerThan(input1, input2) ? input1 : input2;
    }

    public static BigDecimal getGreaterBetween(BigDecimal input1, BigDecimal input2) {
        if (input1 == null) return input2;
        if (input2 == null) return input1;

        return greaterThan(input1, input2) ? input1 : input2;
    }


    public static BigDecimal divide(BigDecimal input1, BigDecimal divisor) {
        return divide(input1, divisor, RoundingMode.HALF_EVEN);
    }

    public static BigDecimal divide(BigDecimal input1, BigDecimal divisor, RoundingMode roundingMode) {
        if (divisor == null || equalsTo(divisor, BigDecimal.ZERO)) return null;
        return input1.divide(divisor, 3, roundingMode);
    }


    public static BigDecimal divideToInteger(BigDecimal input1, BigDecimal divisor) {
        if (divisor == null || equalsTo(divisor, BigDecimal.ZERO)) return null;
        return new BigDecimal(input1.divide(divisor, 3, BigDecimal.ROUND_HALF_EVEN).intValue());
    }


    public static BigDecimal divideAndRoundToInteger(BigDecimal input1, BigDecimal divisor) {
        return divideAndRoundToInteger(input1, divisor, RoundingMode.HALF_EVEN);
    }

    public static BigDecimal divideAndRoundToInteger(BigDecimal input1, BigDecimal divisor, RoundingMode roundingMode) {
        if (divisor == null || equalsTo(divisor, BigDecimal.ZERO)) return null;
        return new BigDecimal(input1.divide(divisor, 0, roundingMode).intValue());
    }

    public static BigDecimal multiply(BigDecimal input1, BigDecimal multiplier) {
        if (input1 == null || multiplier == null) return null;
        return input1.multiply(multiplier);
    }

    public static BigDecimal round(BigDecimal input) {
        if (input == null) return null;
        return input.setScale(CommonConst.Config.NUMBER_OF_DECIMAL_DIGITS, RoundingMode.HALF_EVEN);
    }

    public static boolean isNullOrZero(BigDecimal input) {
        return input == null || equalsTo(input, BigDecimal.ZERO);
    }

}
