package com.red.circle.other.inner.model.dto.material.props;

import com.red.circle.tool.core.num.ArithmeticUtils;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * 道具价格.
 *
 * @author pengliang on 2021/6/1
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class PropsPrice implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;

  /**
   * 天数.
   */
  private Integer days;

  /**
   * 金额.
   */
  private BigDecimal amount;

  /**
   * 折后金额.
   */
  private BigDecimal discountAmount;


  public static List<PropsPrice> of(String validDays, BigDecimal amount, BigDecimal discountRatio) {
    return getDaysStream(validDays).map(Integer::valueOf)
        .map(days -> of(days, amount, discountRatio))
        .collect(Collectors.toList());
  }

  public static List<PropsPrice> of(String validDays, BigDecimal amount) {
    return getDaysStream(validDays).map(Integer::valueOf)
        .map(days -> of(days, amount))
        .collect(Collectors.toList());
  }

  private static Stream<String> getDaysStream(String validDays) {
    return Arrays.stream(validDays.split(","));
  }

  public static PropsPrice of(Integer days, BigDecimal amount, BigDecimal discountRatio) {
    BigDecimal unitPrice = ArithmeticUtils.toBigDecimal(days)
        .multiply(amount)
        .setScale(0, RoundingMode.DOWN);
    return new PropsPrice(days,
        unitPrice,
        unitPrice.multiply(discountRatio).setScale(0, RoundingMode.DOWN));
  }

  public static PropsPrice of(Integer days, BigDecimal amount) {
    return new PropsPrice(days,
        ArithmeticUtils.toBigDecimal(days).multiply(amount).setScale(0, RoundingMode.DOWN));
  }

  public PropsPrice(Integer days, BigDecimal amount) {
    this.days = days;
    this.amount = amount;
  }

  public PropsPrice(Integer days, BigDecimal amount, BigDecimal discountAmount) {
    this.days = days;
    this.amount = amount;
    this.discountAmount = discountAmount;
  }
}
