using System;

namespace JiaZhiQuan.Common.Utils
{
    public static class AmountUtils
    {
        /// <summary>
        /// 转换 元 -> 分
        /// </summary>
        /// <param name="amount"></param>
        /// <returns></returns>
        public static int ConvertYuanToCent(decimal amount)
        {
            return (int)(amount * 100);
        }

        /// <summary>
        /// 百分比转小数
        /// </summary>
        /// <param name="cent"></param>
        /// <returns></returns>
        public static decimal ConvertPercentToDeciaml(decimal d)
        {
            return d / 100;
        }

        /// <summary>
        /// 小数转百分比(保留两位小数)
        /// </summary>
        /// <param name="cent"></param>
        /// <returns></returns>
        public static string ConvertDeciamlToPercent(decimal d)
        {
            return $"{AmountUtils.ConvertRoundReserveTwoDecimal(d * 100).ToString("0.00")}%";
        }

        /// <summary>
        /// 分转元(不会牵扯到四舍五入)
        /// </summary>
        /// <param name="cent"></param>
        /// <returns></returns>
        public static decimal ConvertCentToYuan(decimal cent)
        {
            return cent / 100m;
        }

        /// <summary>
        /// 分转元(保留两位小数, 不会牵扯到四舍五入)
        /// </summary>
        /// <param name="cent"></param>
        /// <returns></returns>
        public static string ConvertCentToYuanStr(decimal cent)
        {
            return ConvertCentToYuan(cent).ToString("0.00");
        }

        /// <summary>
        /// 四舍五入，保留两位小数
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static decimal ConvertRoundReserveTwoDecimal(decimal d)
        {
            return Math.Round(d, 2, MidpointRounding.AwayFromZero);
        }

        /// <summary>
        /// 四舍五入，取整
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static int ConvertRoundReserveRound(decimal d)
        {
            return (int)Math.Round(d, 0, MidpointRounding.AwayFromZero);
        }

        /// <summary>
        /// 计算税费(四舍五入取整)
        /// </summary>
        /// <param name="amount">总金额(分)</param>
        /// <param name="taxRate">税率</param>
        /// <returns></returns>
        public static int ComputeTaxAmount(int amount, decimal taxRate)
        {
            // (四舍五入保留整数)总税额 = 总金额(分) * 税率
            return AmountUtils.ConvertRoundReserveRound(amount * taxRate);
        }

        /// <summary>
        /// 计算服务费(四舍五入取整)
        /// </summary>
        /// <param name="amount">总金额(分)</param>
        /// <param name="taxRate">服务费率</param>
        /// <returns></returns>
        public static int ComputeFeeAmount(int amount, decimal feeRate)
        {
            // (四舍五入保留整数)总服务费 = 总金额(分) * 服务费率
            return AmountUtils.ConvertRoundReserveRound(amount * feeRate);
        }

        /// <summary>
        /// 计算真实金额
        /// </summary>
        /// <param name="amount">总金额(分)</param>
        /// <param name="taxRate">税率</param>
        /// <param name="feeRate">服务费率</param>
        /// <param name="feeMinAmount">服务费最低金额(分)</param>
        /// <returns></returns>
        public static int ComputeRealAmount(int amount, decimal taxRate, decimal feeRate, int feeMinAmount)
        {
            // 去税总金额(分) = 总金额(分) - 税费
            int amount1 = amount - ComputeTaxAmount(amount, taxRate);
            // 打款总金额(分) = 去税总金额(分) - 服务费(服务费有最低限制，如果计算的服务费小于最低限制，那么使用最低限制)
            int amount2 = amount1 - Math.Max(ComputeFeeAmount(amount1, feeRate), feeMinAmount);
            return amount2;
        }
    }

}
