package it.integry.integrywmsnative.core.utility;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

import it.integry.integrywmsnative.core.CommonConst;

public class UtilityNumber {

    private static DecimalFormat decimalFormatInstance;

    public static String decimalToString(Float bigDecimal){
        if(bigDecimal == null) return "0";
        return decimalToString(bigDecimal, CommonConst.Config.NUMBER_OF_DECIMAL_DIGITS);
    }

    public static String decimalToString(BigDecimal bigDecimal){
        if(bigDecimal == null) return "0";
        return decimalToString(bigDecimal, CommonConst.Config.NUMBER_OF_DECIMAL_DIGITS);
    }

    public static String decimalToString(BigDecimal bigDecimal, int decimal){
        if(bigDecimal == null) return "0";
        return decimalToString(bigDecimal.floatValue(), decimal);
    }

    public static String decimalToString(float floatValue, int decimal) {
        DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault());
        otherSymbols.setDecimalSeparator('.');
        otherSymbols.setGroupingSeparator(',');

        final DecimalFormat decimalFormat = new DecimalFormat();
        decimalFormat.setMaximumFractionDigits(decimal);
        decimalFormat.setDecimalFormatSymbols(otherSymbols);
        decimalFormat.setGroupingUsed(false);
        decimalFormat.setMinimumFractionDigits(Math.min(2, decimal));

        return decimalFormat.format(floatValue);
    }

    public static DecimalFormat getNumberFormatInstance() {

        if(decimalFormatInstance == null) {
            DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault());
            otherSymbols.setDecimalSeparator('.');
            otherSymbols.setGroupingSeparator(',');

            decimalFormatInstance = new DecimalFormat();
            decimalFormatInstance.setMaximumFractionDigits(CommonConst.Config.NUMBER_OF_DECIMAL_DIGITS);
            decimalFormatInstance.setDecimalFormatSymbols(otherSymbols);
            decimalFormatInstance.setGroupingUsed(false);
        }

        return decimalFormatInstance;
    }

    public static int countNumberOfDecimalDigits(Float value) {
        String text = getNumberFormatInstance().format(Math.abs(value));
        int integerPlaces = text.indexOf('.');
        int decimalPlaces = text.length() - integerPlaces - 1;

        return decimalPlaces;
    }

    public static String normalizeStringNumber(String numberString) {
        return numberString.replaceAll(",", ".");
    }




    public static Float decimalToFloat(BigDecimal bigDecimalValue) {
        float multiplier = (float) Math.pow(10, CommonConst.Config.NUMBER_OF_DECIMAL_DIGITS);

        Float floatValue = null;

        if(bigDecimalValue != null) {
            int intValue = (int) (bigDecimalValue.floatValue() * multiplier);
            floatValue = Float.valueOf(intValue / multiplier);
        }

        return floatValue;
    }

    public static float truncateToDecimal(float v, int i) {
        int delta = 10*i;

        return ((int)v*delta)/delta;

    }
}
