package com.gingersoft.gsa.cloud.base.utils;


import com.elvishew.xlog.XLog;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import static java.math.BigDecimal.ROUND_HALF_UP;

/**
 * Created by Administrator on 2018/9/10 0010.
 */

public class MoneyUtil {

    // 總金額小數位
    public static int rounding, RoundingDecimal;
    // (直接截取) :  食品   細項   折扣  會員折扣  合計  服務費
    public static int ItemDecimals = 1;

    public static double get_ItemDecimals_money(double money) {
        double rMoney = money;
        //返回指定的小数位：
        //e.g Coke $13.456
        //ItemDecimals=0 $13.00
        //ItemDecimals=1 $13.40
        //ItemDecimals=2 $13.45
        rMoney = new BigDecimal(String.valueOf(money)).setScale(ItemDecimals, BigDecimal.ROUND_DOWN).doubleValue();
        return rMoney;
    }

    public static double get_Count_money(double countmoney) {

        //计算并返回最后的符合规定的取值
        XLog.tag("MoneyUtil").d("计算账单小数前金额get_Count_money: " + countmoney);

        //帐单小数
        //Sys_AndroidSetting \Rounding 帳單小數算法
        //例如: e.g
        //Rounding=0  0.1-0.9=0.0 /
        //Rounding=1  0.1-0.9=1.0 /
        //Rounding=2  0.1-0.4=0.0 and 0.5-0.9=1.0  即 4 捨 5 入
        //Rounding=3  0.1-0.5=0.5 and 0.6-0.9=1.0

        //Sys_AndroidSetting \RoundingDecimal 整單的小數位
        //例如:帳單總金額 13.456
        //RoundingDecimal=1   小數後1個位進行四捨五入, 總金額結果= 13.00
        //RoundingDecimal=2   小數後2個位進行四捨五入, 總金額結果= 13.50

        //12.25	                                                   RoundingDecimal=1	RoundingDecimal=2	RoundingDecimal=3
        //Rounding=0(0.1-0.9=0.0)	                                     12.00          	12.20	             12.25
        //Rounding=1(0.1-0.9=1.0)      	                                 13.00	            12.30	             12.25
        //Rounding=2(0.1-0.4=0.0 and 0.5-0.9=1.0)                  	     12.00				12.30				 12.25
        //Rounding=3(0.1-0.5=0.0 and 0.6-0.9=1.0)	                     12.00             	12.20	             12.25


        double dMoney = 0.00;
        //Locale.setDefault(Locale.GERMAN);

        //	 countmoney=round(countmoney, RoundingDecimal+1);//首先产生包含了正确的数值
        DecimalFormat df = new DecimalFormat("#.000000000");

        if (countmoney < 1) {
            countmoney = Math.round(countmoney);
        }

        String cm = df.format(countmoney);

        String[] stringarray = cm.split("[.]");

        String Int_v = stringarray[0]; //整数部分
        String litt_v = stringarray[1];//小数部分
        if (Int_v.length() == 0) {
            return 0.00;
        }

        int endIndex = RoundingDecimal - 1;

        if (litt_v.length() <= endIndex) {
            return countmoney;
        }

        String midd = litt_v.substring(0, RoundingDecimal - 1);
        String mid = litt_v.substring(RoundingDecimal - 1, RoundingDecimal);

        switch (rounding) {
            case 0:
                //计算出countmoney的值
                mid = "0";
                mid = Int_v + "." + midd + mid;
                dMoney = Double.parseDouble(mid);
                break;
            case 1://取出来的位置如果大于0，则需要在前面加1
                if (!mid.equals("0")) {
                    if (RoundingDecimal == 1) {
                        Int_v = String.valueOf(Integer.parseInt(Int_v) + 1);
                        mid = "0";
                    } else {
                        midd = String.valueOf(Integer.parseInt(midd) + 1);
                        mid = "0";
                    }
                }
                mid = Int_v + "." + midd + mid;
                dMoney = Double.parseDouble(mid);
                break;
            case 2:
                if (!mid.equals("0")) {
                    if (Integer.parseInt(mid) > 4) {
                        if (RoundingDecimal == 1) {
                            Int_v = String.valueOf(Integer.parseInt(Int_v) + 1);
                            mid = "0";
                        } else {
                            midd = String.valueOf(Integer.parseInt(midd) + 1);
                            mid = "0";
                        }
                    } else {
                        //小于4不进位
                        mid = "0";
                    }
                }
                mid = Int_v + "." + midd + mid;
                dMoney = Double.parseDouble(mid);
                break;
            case 3:
                if (!mid.equals("0")) {
                    if (Integer.parseInt(mid) > 5) {
                        if (RoundingDecimal == 1) {
                            Int_v = String.valueOf(Integer.parseInt(Int_v) + 1);
                            mid = "0";
                        } else {
                            midd = String.valueOf(Integer.parseInt(midd) + 1);
                            mid = "0";
                        }
                    } else {
                        //小于5不进位
                        mid = "5";
                    }
                }
                mid = Int_v + "." + midd + mid;
                dMoney = Double.parseDouble(mid);
                break;
            default:
                dMoney = Double.parseDouble(new DecimalFormat("#.00").format(countmoney));
                break;
        }

        XLog.tag("MoneyUtil").d("计算账单小数后金额get_Count_money: " + dMoney);

        return dMoney;
    }


    public static double get_Count_money2(double countmoney) {

        //计算并返回最后的符合规定的取值
        XLog.tag("MoneyUtil").d("计算账单小数前金额get_Count_money: " + countmoney);

        double dMoney = 0.00;

        DecimalFormat df = new DecimalFormat("#.000000000");

        if (countmoney < 1) {
            countmoney = Math.round(countmoney);
        }

        String cm = df.format(countmoney);

        String[] stringarray = cm.split("[.]");

        String Int_v = stringarray[0]; //整数部分
        String litt_v = stringarray[1];//小数部分
        if (Int_v.length() == 0) {
            return 0.00;
        }

        int endIndex = RoundingDecimal - 1;

        if (litt_v.length() <= endIndex) {
            return countmoney;
        }

        String midd = litt_v.substring(0, RoundingDecimal - 1);
        String mid = litt_v.substring(RoundingDecimal - 1, RoundingDecimal);

        switch (rounding) {
            case 0:
                //计算出countmoney的值
                mid = "0";
                mid = Int_v + "." + midd + mid;
                dMoney = Double.parseDouble(mid);
                break;
            case 1://取出来的位置如果大于0，则需要在前面加1
                if (!mid.equals("0")) {
                    if (RoundingDecimal == 1) {
                        Int_v = String.valueOf(Integer.parseInt(Int_v) + 1);
                        mid = "0";
                    } else {
                        midd = String.valueOf(Integer.parseInt(midd) + 1);
                        mid = "0";
                    }
                }
                mid = Int_v + "." + midd + mid;
                dMoney = Double.parseDouble(mid);
                break;
            case 2:
                if (!mid.equals("0")) {
                    if (Integer.parseInt(mid) > 4) {
                        if (RoundingDecimal == 1) {
                            Int_v = String.valueOf(Integer.parseInt(Int_v) + 1);
                            mid = "0";
                        } else {
                            midd = String.valueOf(Integer.parseInt(midd) + 1);
                            mid = "0";
                        }
                    } else {
                        //小于4不进位
                        mid = "0";
                    }
                }
                mid = Int_v + "." + midd + mid;
                dMoney = Double.parseDouble(mid);
                break;
            case 3:
                if (!mid.equals("0")) {
                    if (Integer.parseInt(mid) > 5) {
                        if (RoundingDecimal == 1) {
                            Int_v = String.valueOf(Integer.parseInt(Int_v) + 1);
                            mid = "0";
                        } else {
                            midd = String.valueOf(Integer.parseInt(midd) + 1);
                            mid = "0";
                        }
                    } else {
                        //小于5不进位
                        mid = "5";
                    }
                }
                mid = Int_v + "." + midd + mid;
                dMoney = Double.parseDouble(mid);
                break;
        }

        XLog.tag("MoneyUtil").d("计算账单小数后金额get_Count_money: " + dMoney);

        return dMoney;
    }

    public static double getDecimalMoney(double money, int decimal) {
        return new BigDecimal(String.valueOf(money)).setScale(decimal, ROUND_HALF_UP).doubleValue();
    }

    /**
     * 计算总价
     * 乘
     *
     * @param price
     * @param number
     * @return
     */
    public static double priceCalculation(double price, int number) {
        BigDecimal b1 = new BigDecimal(price);
        BigDecimal b2 = new BigDecimal(number);
        return (b1.multiply(b2)).setScale(1, ROUND_HALF_UP).doubleValue();
    }

    /**
     * 计算总价
     * 計算和
     *
     * @param price
     * @param number
     * @return
     */
    public static double sum(double price, double number) {
        BigDecimal b1 = new BigDecimal(Double.toString(price));
        BigDecimal b2 = new BigDecimal(Double.toString(number));
        return b1.add(b2).doubleValue();
    }

    /**
     * 计算总价
     * 計算和
     *
     * @param price
     * @param price2
     * @return
     */
    public static BigDecimal sum(BigDecimal price, double price2) {
        BigDecimal b2 = new BigDecimal(Double.toString(price2));
        return price.add(b2);
    }

    /**
     * 計算差
     *
     * @param v1
     * @param v2
     * @return
     */
    public static double sub(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).doubleValue();
    }

    /**
     * 計算差
     *
     * @param v1
     * @param v2
     * @return
     */
//    public static double sub(String v1, double v2) {
//        BigDecimal b1 = new BigDecimal(Double.toString(v1));
//        BigDecimal b2 = new BigDecimal(Double.toString(v2));
//        return b1.subtract(b2).doubleValue();
//    }

    /**
     * 計算除 保留兩位小數，四捨五入
     *
     * @param v1
     * @param v2
     * @return
     */
    public static BigDecimal divide(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, 2, ROUND_HALF_UP);
    }

    /**
     * 計算除
     * @param v1
     * @param v2
     * @param scale 保留幾位小數
     * @param RoundingMode ROUND_HALF_UP為四捨五入
     * @return
     */
    public static float divide(double v1, double v2, int scale, int RoundingMode) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, RoundingMode).floatValue();
    }

    /**
     * 計算除
     * @param v1
     * @param v2
     * @param scale 保留幾位小數
     * @return
     */
    public static float divide(double v1, double v2, int scale) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, ROUND_HALF_UP).floatValue();
    }

    /**
     * 計算差
     *
     * @param v1
     * @param v2
     * @return
     */
    public static int sub(int v1, int v2) {
        BigDecimal b1 = new BigDecimal(String.valueOf(v1));
        BigDecimal b2 = new BigDecimal(String.valueOf(v2));
        return b1.subtract(b2).intValue();
    }

    /**
     * 食品总价保留两位
     *
     * @param totalPrice
     * @return
     */
    public static double totalFoodPrice(double totalPrice) {
        BigDecimal b = new BigDecimal(totalPrice);
        return b.setScale(1, ROUND_HALF_UP).doubleValue();
    }
}
