package yem.base.common.utils;

import java.math.BigDecimal;

/**
 * 金额转英文大写工具类
 * Example ：
 * 调用示例：
 * String desc = AmountToEnDesUtil.convertEnDes(new BigDecimal("100.55"), "say", "RMB", "only"));
 * 返回 desc为"say one hundred RMB and fifty five cents only"
 *
 * @author
 */
public class RMBEnBigAmt {

    private static final String[] majorNames = {" ", " thousand ",
            " million ", " billion ", " trillion ", " quadrillion ",
            " quintillion "};
    private static final String[] tensNames = {" ", " ten ", " twenty ",
            " thirty ", " forty ", " fifty ", " sixty ", " seventy ",
            " eighty ", " ninety "};
    private static final String[] numNames = {" ", " one ", " two ",
            " three ", " four ", " five ", " six ", " seven ", " eight ",
            " nine ", " ten ", " eleven ", " twelve ", " thirteen ",
            " fourteen ", " fifteen ", " sixteen ", " seventeen ",
            " eighteen ", " nineteen "};

    public static String convertEnDes(BigDecimal val, String prefix,
                                      String currencyISO, String suffix) {
        if (val == null)
            return "";
        StringBuffer sb = new StringBuffer();
        sb.append(prefix).append(" ");
        String[] amtEnDesc = getAmtEnDesc(val);
        sb.append(currencyISO).append(" ");
        sb.append(amtEnDesc[0]).append(" ");
        if (amtEnDesc[1].trim().length() != 0
                && !"zero".equalsIgnoreCase(amtEnDesc[1].trim())) {
            sb.append("and ").append(amtEnDesc[1]).append(" cents ");
        }
        sb.append(suffix).append(" ");
        return sb.toString().toUpperCase();
    }

    private static String convertLessThanOneThousand(long number) {
        String soFar;

        if (number % 100 < 20) {
            soFar = numNames[(int) (number % 100)];
            number /= 100;
        } else {
            soFar = numNames[(int) (number % 10)];
            number /= 10;

            soFar = tensNames[(int) (number % 10)] + soFar;
            number /= 10;
        }
        if (number == 0)
            return soFar;
        return numNames[(int) number] + "hundred " + soFar;
    }

    public static String convert(long number) {

        if (number == 0) {
            return "zero ";
        }

        String prefix = " ";

        if (number < 0) {
            number = -number;
            prefix = "negative ";
        }

        String soFar = " ";
        int place = 0;

        do {
            long n = (number % 1000);
            if (n != 0) {
                String s = convertLessThanOneThousand(n);
                soFar = s + majorNames[place] + soFar;
            }
            place++;
            number /= 1000;
        } while (number > 0);

        return (prefix + soFar).trim();
    }

    private static String[] getAmtEnDesc(BigDecimal val) {

        String[] ret = new String[2];

        val = val.setScale(2, BigDecimal.ROUND_HALF_UP);
        String valDes = String.valueOf(val);
        String[] valSplit = valDes.split("\\.");
        //int part0 = Integer.parseInt(valSplit[0]);
        long part0 = Long.parseLong(valSplit[0]);
        ret[0] = convert(part0);

        try {
            int part1 = Integer.parseInt(valSplit[1]);
            ret[1] = convert(part1);
        } catch (Exception ex) {
            ret[1] = "";
        }
        return ret;
    }
}
