package leigh.minecraft;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;

/**
 * Utility related class for currency (marble) related functionality.
 *
 * @author Alex Leigh
 */
public class MarblesUtil {
    private static final NumberFormat marbleFormatter;

    static {
        marbleFormatter = NumberFormat.getCurrencyInstance();
        DecimalFormatSymbols dfs = new DecimalFormatSymbols();
        dfs.setCurrencySymbol("M");
        dfs.setGroupingSeparator('.');
        dfs.setMonetaryDecimalSeparator('.');
        ((DecimalFormat) marbleFormatter).setDecimalFormatSymbols(dfs);
    }

    public static String formatCents(int cents) {
        StringBuffer sb = new StringBuffer();
        sb.append("M");
        sb.append(cents / 100);
        int rem = cents % 100;
        if (rem > 0) {
            sb.append(".");
            sb.append(cents % 100);
        }
        return sb.toString();
    }

    /**
     * Convert a double currency value into a formatted Marbles string. Note that using this method is not recommended
     * due to issues with floating point rounding. As a result, it is listed as deprecated.
     */
    @Deprecated
    public static String formatMarbles(double amount) {
        return marbleFormatter.format(amount);
    }

    /**
     * Convert a double currency value into a formatted Marbles string. Note that using this method is not recommended
     * due to issues with floating point rounding. As a result, it is listed as deprecated.
     */
    @Deprecated
    public static String formatGeneric(double amount) {
        // I am self-aware that this is reprehensible.
        return marbleFormatter.format(amount).substring(1);
    }
}
