package com.wd.workoffice.util;

import android.text.TextUtils;
import android.util.Log;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;

import freemarker.template.utility.CollectionUtils;

/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MathUtils {
    public static String conver(int amount) {
        String str = "";
        if (amount < 0 || amount > 99) {
            str = "不合法的数字";
        } else if (amount >= 1 && amount <= 10) {
            str = cast(amount);
        } else if (amount >= 11 && amount <= 19) {
            int num = amount % 10;
            str = "十" + cast(num);
        } else if (amount >= 20 && amount <= 99) {
            int num1 = amount / 10 % 10;
            int num2 = amount % 10;
            str = cast(num1) + "十" + cast(num2);
        }

        return str;

    }

    public static String cast(int num) {
        String str = "";
        switch (num) {
            case 0:
                str = "";
                break;
            case 1:
                str = "一";
                break;
            case 2:
                str = "二";
                break;
            case 3:
                str = "三";
                break;
            case 4:
                str = "四";
                break;
            case 5:
                str = "五";
                break;
            case 6:
                str = "六";
                break;
            case 7:
                str = "七";
                break;
            case 8:
                str = "八";
                break;
            case 9:
                str = "九";
                break;
            case 10:
                str = "十";
                break;
        }
        return str;

    }

    public static String converData(String value, Integer scale) {
        if (TextUtils.isEmpty(value)) {
            value = "0";
        }
        BigDecimal bg = new BigDecimal(value);
        return bg.setScale(scale, RoundingMode.HALF_UP).toString();
    }

    public static String converData(double value, Integer scale) {
        String strValue;
        if (value == 0d || TextUtils.isEmpty(String.valueOf(value))) {
            strValue = "0";
        } else {
            strValue = String.valueOf(value);
        }
        BigDecimal bg = new BigDecimal(strValue);
        return bg.setScale(scale, RoundingMode.HALF_UP).toString();
    }


    public static String multiply(String v1, String v2, Integer scale) {
        if (TextUtils.isEmpty(v1)) {
            v1 = "0";
        }
        if (TextUtils.isEmpty(v2)) {
            v2 = "0";
        }
        BigDecimal b1 = new BigDecimal(v1.trim());
        BigDecimal b2 = new BigDecimal(v2.trim());
        return b1.multiply(b2).setScale(scale).toString();
    }

    public static String add(String v1, String v2, Integer scale) {
        if (TextUtils.isEmpty(v1)) {
            v1 = "0";
        }
        if (TextUtils.isEmpty(v2)) {
            v2 = "0";
        }
        BigDecimal b1 = new BigDecimal(v1.trim());
        BigDecimal b2 = new BigDecimal(v2.trim());
        return b1.add(b2).setScale(scale).toString();
    }

    public static String subtract(String v1, String v2, Integer scale) {
        if (TextUtils.isEmpty(v1)) {
            v1 = "0";
        }
        if (TextUtils.isEmpty(v2)) {
            v2 = "0";
        }
        BigDecimal b1 = new BigDecimal(v1.trim());
        BigDecimal b2 = new BigDecimal(v2.trim());
        return b1.subtract(b2).setScale(scale).toString();
    }
    public static String divide(String v1, String v2, Integer scale) {
        if (TextUtils.isEmpty(v1)) {
            v1 = "0";
        }
        if (TextUtils.isEmpty(v2)) {
            v2 = "0";
        }
        BigDecimal b1 = new BigDecimal(v1.trim());
        BigDecimal b2 = new BigDecimal(v2.trim());
        return b1.divide(b2).setScale(scale).toString();
    }


}
