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


import com.elvishew.xlog.XLog;

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

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

public class MoneyUtil {

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

    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, BigDecimal.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, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
