package com.fizz.business.utils;

import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;


@Component
public class NumberUtil {

    public static double add(double number1, double number2) {
        BigDecimal bignum1 = new BigDecimal(String.valueOf(number1));
        BigDecimal bignum2 = new BigDecimal(String.valueOf(number2));
        BigDecimal bignum3 = bignum1.add(bignum2);
        double result = bignum3.doubleValue();
        return result;
    }

    public static BigDecimal add(BigDecimal number1, BigDecimal number2) {
        BigDecimal result = number1.add(number2);
        return result;
    }

    public static double subtract(double number1, double number2) {
        BigDecimal bignum1 = new BigDecimal(String.valueOf(number1));
        BigDecimal bignum2 = new BigDecimal(String.valueOf(number2));
        BigDecimal bignum3 = bignum1.subtract(bignum2);
        double result = bignum3.doubleValue();
        return result;
    }

    public static BigDecimal subtract(BigDecimal number1, BigDecimal number2) {
        BigDecimal result = number1.subtract(number2);
        return result;
    }

    public static double multiply(double number1, double number2) {
        BigDecimal bignum1 = new BigDecimal(String.valueOf(number1));
        BigDecimal bignum2 = new BigDecimal(String.valueOf(number2));
        BigDecimal bignum3 = bignum1.multiply(bignum2);
        double result = bignum3.doubleValue();
        return result;
    }

    public static BigDecimal multiply(BigDecimal number1, BigDecimal number2) {
        BigDecimal result = number1.multiply(number2);
        return result;
    }

    public static double divide(double number1, double number2, int scale) {
        BigDecimal bignum1 = new BigDecimal(String.valueOf(number1));
        BigDecimal bignum2 = new BigDecimal(String.valueOf(number2));
        if (number2 == 0.0) {
            return 0.0;
        } else {
            double result = bignum1.divide(bignum2, scale, 4).doubleValue();
            return result;
        }
    }

    public static BigDecimal divide(BigDecimal number1, BigDecimal number2, int scale) {
        if (number2.compareTo(BigDecimal.ZERO) == 0) {
            return new BigDecimal("0");
        } else {
            BigDecimal result = number1.divide(number2, scale, 4);
            return result;
        }
    }

    public static double divideRate(double number1, double number2, int scale) {
        BigDecimal bignum1 = new BigDecimal(String.valueOf(number1));
        BigDecimal bignum2 = new BigDecimal(String.valueOf(number2));
        if (number2 == 0.0) {
            return 0.0;
        } else {
            double result = formatDouble(bignum1.divide(bignum2, scale, 4).doubleValue() * 100.0, 2);
            return result;
        }
    }

    public static BigDecimal divideRate(BigDecimal number1, BigDecimal number2, int scale) {
        if (number2.compareTo(BigDecimal.ZERO) == 0) {
            return new BigDecimal("0");
        } else {
            BigDecimal result = number1.divide(number2, scale, 4).multiply(new BigDecimal("100"));
            return result;
        }
    }

    public static double formatDouble(double number, int scale) {
        BigDecimal bigDecimal = new BigDecimal(String.valueOf(number));
        return bigDecimal.setScale(scale, 4).doubleValue();
    }

    public static BigDecimal formatBigDecimal(BigDecimal number, int scale) {
        return number.setScale(scale, 4);
    }

    public static BigDecimal doubleTransferBigDecimal(double number) {
        BigDecimal bigDecimal = new BigDecimal(String.valueOf(number));
        return bigDecimal;
    }

    public static double BigDecimalTransferDouble(BigDecimal number) {
        double result = number.doubleValue();
        return result;
    }

    public static double divideForList(List<Double> list) {
        Double resultAvg = (Double)list.stream().collect(Collectors.averagingDouble((item) -> {
            return item;
        }));
        return resultAvg;
    }

}
