package com.red.circle.mq.business.model.event.gift;

import com.google.common.collect.Lists;
import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.tool.core.num.ArithmeticUtils;
import com.red.circle.tool.core.text.StringUtils;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * 礼物赠送消息模型 on 2022/7/25.
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
public class GiveAwayGiftBatchEvent implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;
  private static final String GIFT_TAB_CP = "CP";
  private static final String GIFT_TYPE_GOLD = "GOLD";
  private static final String GIFT_TYPE_DIAMOND = "DIAMOND";


  /**
   * 第三方弹幕游戏ID
   */
  private Integer gameId;

  /**
   * 归属系统.
   */
  @NotNull(message = "sysOrigin required.")
  private SysOriginPlatformEnum sysOrigin;

  /**
   * 请求平台.
   */
  @NotBlank(message = "requestPlatform required.")
  private String requestPlatform;

  /**
   * 用户id.
   */
  @NotNull(message = "sendUserId required.")
  private Long sendUserId;

  /**
   * 接收信息.
   */
  @NotNull(message = "accepts required.")
  private List<GiveAwayGiftRoomAccepts> accepts;

  /**
   * 礼物数量.
   */
  @NotNull(message = "quantity required")
  private Integer quantity;

  /**
   * 礼物配置.
   */
  @NotNull(message = "giftConfig required")
  private GiveGiftConfig giftConfig;

  /**
   * 房间id.
   */
  private Long roomId;

  /**
   * 跟踪id.
   */
  @NotNull(message = "giftConfig required.")
  private Long trackId;

  /**
   * true 背包礼物.
   */
  @NotNull(message = "bagGift required.")
  private Boolean bagGift;

  /**
   * 连续点击幸运礼物次数.
   */
  private Integer luckyGiftCount;

  /**
   * 是否是重试消息.
   * private Boolean retry;
   */

  /**
   * 创建时间.
   */
  @NotNull(message = "createTime required.")
  private Timestamp createTime;

  /**
   * 沙特日期.
   */
  private Integer createDateRiyadhNum;

  /**
   * 已点歌曲唯一标识ID(不是歌曲配置id而是已点歌曲id).
   */
  private Long songId;

  /**
   * 是否为祝福礼物:true是,其他不是.
   */
  private Boolean checkBlessingsGift;

  /**
   * 获取接收有用户.
   */
  private List<Long> acceptUserIds() {
    return Optional.ofNullable(accepts)
        .map(acceptUsers -> acceptUsers.stream().map(GiveAwayGiftRoomAccepts::getAcceptUserId)
            .filter(Objects::nonNull).distinct().toList())
        .orElse(Lists.newArrayList());
  }

  /**
   * 获取所有用户包含发送用户.
   */
  public Set<Long> allUserId() {
    return Stream.of(acceptUserIds(), Collections.singletonList(sendUserId))
        .flatMap(Collection::stream)
        .collect(Collectors.toSet());
  }

  /**
   * 接收人数.
   */
  public Integer acceptUserSize() {
    return acceptUserIds().size();
  }

  /**
   * 是否是金币类型礼物.
   */
  public boolean checkGiftTypeEqGold() {
    return Objects.nonNull(giftConfig) && Objects
        .equals(giftConfig.getType(), GIFT_TYPE_GOLD);
  }

  /**
   * 是否是钻石类型礼物.
   */
  public boolean checkGiftTypeEqDiamond() {
    return Objects.nonNull(giftConfig) && Objects
        .equals(giftConfig.getType(), GIFT_TYPE_DIAMOND);
  }

  /**
   * 获取礼物类型.
   */
  public String giftType() {
    return giftConfig.getType();
  }

  /**
   * 是否是cp礼物.
   */
  public boolean checkCpGifts() {
    return Objects.nonNull(giftConfig) && Objects
        .equals(giftConfig.getGiftTab(), GIFT_TAB_CP);
  }

  /**
   * 赠送总额.
   */
  public BigDecimal countConsumAmountTotal() {
    if (ArithmeticUtils.lteZero(giftConfig.getGiftCandy())) {
      return BigDecimal.ZERO;
    }
    return giftConfig.getGiftCandy().multiply(BigDecimal.valueOf(quantity))
        .multiply(BigDecimal.valueOf(acceptUserSize()));
  }

  /**
   * 可获得积分钻石.
   */
  public BigDecimal countSingleGiftDiamond() {
    if (ArithmeticUtils.lteZero(giftConfig.getGiftIntegral())) {
      return BigDecimal.ZERO;
    }
    return giftConfig.getGiftIntegral().multiply(BigDecimal.valueOf(quantity));
  }

  /**
   * 单个礼物金币.
   */
  public BigDecimal countSingleGiftGolds() {
    if (ArithmeticUtils.lteZero(giftConfig.getGiftCandy())) {
      return BigDecimal.ZERO;
    }
    return giftConfig.getGiftCandy().multiply(BigDecimal.valueOf(quantity));
  }

  /**
   * 是否是周星礼物.
   */
  public boolean checkWeekStar() {
    return StringUtils.isNotBlank(giftConfig.getSpecial()) && giftConfig.getSpecial().contains(
        "STAR");
  }

  /**
   * 是否是周星礼物.
   */
  public boolean checkWeekKingQueen() {
    return StringUtils.isNotBlank(giftConfig.getSpecial()) && giftConfig.getSpecial().contains(
        "KING_QUEEN");
  }

  /**
   * 是否是幸运礼物.
   */
  public boolean checkLuckyGift() {
    return Objects.nonNull(giftConfig) && Objects.equals(giftConfig.getGiftTab(), "LUCKY_GIFT");
  }

  /**
   * 是否是CP礼物.
   */
  public boolean checkCpGift() {
    return Objects.nonNull(giftConfig) && Objects.equals(giftConfig.getGiftTab(), "CP");
  }


  /**
   * 是否是专属礼物.
   */
  public boolean checkExclusive() {
    return Objects.nonNull(giftConfig)
        && Objects.equals(giftConfig.getGiftTab(), "EXCLUSIVE");
  }

  /**
   * 获取礼物ID.
   */
  public Long giftId() {
    return giftConfig.getId();
  }

  public boolean checkAswat() {
    return Objects.equals(getSysOrigin(), SysOriginPlatformEnum.ASWAT);
  }

}
