package in.techware.carrefour.util;

import android.util.Log;
import android.widget.Toast;

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

import in.techware.carrefour.R;
import in.techware.carrefour.app.App;

/**
 * Created by Jemsheer K D on 23 July, 2018.
 * Package in.techware.carrefour.util
 * Project Carrefour
 */
public class BarcodeUtil {

    private static final String TAG = "BarcodeUtil";

    public static String getFormattedBarcode(String barcode) {

        if (barcode.length() > 13) {
            Toast.makeText(App.getInstance(), R.string.message_invalid_barcode,
                    Toast.LENGTH_SHORT).show();
            return "";
        } else if (barcode.length() < 13) {
            int diff = 13 - barcode.length();
//            return String.format(Locale.US, "%0" + diff + "d", barcode);
            String temp = String.format(Locale.US, "%0" + diff + "d", 0);
            temp += barcode;
            return temp;
        } else {
            return barcode;
        }
    }

    public static String getFormattedQuantity(float quantity, int padding, boolean hasDecimal) {

        String str = "";
        if (hasDecimal) {
            str = String.format(Locale.US, "%.3f", quantity);
        } else {
            str = String.valueOf((int) quantity);
        }

        Log.i(TAG, "getFormattedQuantity: Sub String with 3 decimal point" + str);
        str = str.replace(".", "");

        if (str.length() > padding) {
            Toast.makeText(App.getInstance(), R.string.message_invalid_quantity,
                    Toast.LENGTH_SHORT).show();
            return "";
        } else if (str.length() < padding) {
            int diff = padding - str.length();
            String temp = String.format(Locale.US, "%0" + diff + "d", 0);
            temp += str;
            return temp;
//            return String.format(Locale.US, "%0" + diff + "d", quantity);
        } else {
            return str;
        }
    }

    public static String getFormattedPrice(float price) {


        String str = String.format(Locale.US, "%.3f", price);

        Log.i(TAG, "getFormattedPrice: Sub String with 3 decimal point" + str);
        str = str.replace(".", "");

        if (str.length() > 11) {
            Toast.makeText(App.getInstance(), R.string.message_invalid_quantity,
                    Toast.LENGTH_SHORT).show();
            return "";
        } else if (str.length() < 11) {
            int diff = 11 - str.length();
            String temp = String.format(Locale.US, "%0" + diff + "d", 0);
            temp += str;
            return temp;
//            return String.format(Locale.US, "%0" + diff + "d", price);
        } else {
            return str;
        }
    }

    public static String removeTrailingZeroes(float quantity) {

        DecimalFormat df = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
        df.setMaximumFractionDigits(340);
        return df.format(quantity);
    }
}
