package com.red.circle.console.app.dto.bo;

import com.red.circle.other.inner.enums.material.PropsCurrencyType;
import com.red.circle.tool.core.num.ArithmeticUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 处理重复方法道具资源.
 * </p>
 *
 * @author pengshigang
 * @since 2021-06-05
 */
public class PropsBO {

  /**
   * 重新排序拼接天数
   *
   * @param validDays 天数字符串
   * @return 排序后的天数字符串
   */
  public static String getSortDayStr(String validDays) {
    if (StringUtils.isBlank(validDays)) {
      return "";
    }
    List<Integer> daysInt = Arrays.stream(validDays.split(",")).map(Integer::parseInt).sorted()
        .collect(Collectors.toList());
    StringBuilder dayStr = new StringBuilder();
    for (Integer day : daysInt) {
      dayStr.append(day).append(",");
    }
    return dayStr.substring(0, dayStr.length() - 1);
  }

  /**
   * 获得货币类型名称拼接
   *
   * @param currencyTypes 货币类型
   */
  public static String getCurrencyTypesNameStr(String currencyTypes) {
    StringBuilder propsCurrencyTypesName = new StringBuilder();
    for (String currencyType : currencyTypes.split(",")) {
      propsCurrencyTypesName.append(PropsCurrencyType.getDescValue(currencyType)).append(",");
    }
    return propsCurrencyTypesName.substring(0, propsCurrencyTypesName.length() - 1);
  }

  /**
   * 获得天数*低价的拼接字符串
   *
   * @param validDays 天数
   * @param amount    低价
   */
  public static String getDayMoney(String validDays, BigDecimal amount) {
    StringBuilder dayMoney = new StringBuilder();

    for (String day : validDays.split(",")) {
      dayMoney.append(day).append("天:").append(
              ArithmeticUtils.toBigDecimal(day).
                  multiply(amount)
                  .setScale(0, RoundingMode.HALF_UP)
                  .stripTrailingZeros()
                  .toPlainString()
          )
          .append(",");
    }
    return dayMoney.substring(0, dayMoney.length() - 1);
  }

  /**
   * 获得天数*低价*折扣的拼接字符串
   *
   * @param validDays 天数
   * @param amount    低价
   * @param discount  折扣
   */
  public static String getDayDiscountMoney(String validDays, BigDecimal amount,
      BigDecimal discount) {

    StringBuilder dayMoney = new StringBuilder();
    for (String day : validDays.split(",")) {
      dayMoney.append(day).append("天:").append(
          ArithmeticUtils.toBigDecimal(day)
              .multiply(amount)
              .multiply(discount)
              .setScale(0, RoundingMode.HALF_UP)
              .stripTrailingZeros()
              .toPlainString()
      ).append(",");
    }
    return dayMoney.substring(0, dayMoney.length() - 1);
  }


}
