package com.red.circle.other.app.dto.clientobject.pet;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.red.circle.framework.dto.ClientObject;
import com.red.circle.tool.core.date.TimestampUtils;
import java.io.Serial;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.util.List;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * 当前饲养宠物.
 *
 * @author pengliang on 2021/10/14
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class PetCurrentFeedingCO extends ClientObject {

  @Serial
  private static final long serialVersionUID = 1L;

  /**
   * 宠物背包信息，激活后返回.
   */
  private PetBagCO petBag;

  /**
   * 阶段信息.
   */
  private PetStageCO petStage;

  /**
   * true 免费，false 收费.
   */
  private Boolean freeFeeding;

  /**
   * 喂食时间.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long feedingTime;

  /**
   * 喂食进度条.
   */
  private Integer feedingSchedule;

  /**
   * 收益.
   */
  private List<PetIncomeCO> incomes;

  /**
   * true 已养成,false 未养成
   */
  private Boolean finish;

  /**
   * 加速喂养-粮食.
   */
  private BigDecimal accelerateFeed;

  public PetCurrentFeedingCO calculate() {
    this.feedingTime = calculateFeedingTime();
    this.feedingSchedule = calculateFeedingSchedule();
    this.accelerateFeed = calculateAccelerateFeed();
    return this;
  }

  private BigDecimal calculateAccelerateFeed() {
    return petStage.getAccelerateFeed().multiply(BigDecimal.valueOf(petBag.getFeedingNum()))
        .setScale(0, RoundingMode.DOWN);
  }

  private Long calculateFeedingTime() {
    Timestamp time = TimestampUtils.convert(petBag.getLatestFeedingTime().toLocalDateTime()
        .plusMinutes(petStage.getFeedingIntervalMinute()));

    Long interval = TimestampUtils
        .expiredIntervalMillisecond(TimestampUtils.now(), time);
    return interval > 0 ? interval : null;
  }

  private int calculateFeedingSchedule() {
    return (int) (
        Double.valueOf(petBag.getFeedingNum()) / Double.valueOf(petStage.getUpgradeFeedingNum())
            * 100);
  }

}
