package com.red.circle.other.app.command.material;

import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.infra.database.cache.entity.material.UserEmojiBackpackCache;
import com.red.circle.other.infra.database.cache.service.other.EmojiCacheService;
import com.red.circle.other.infra.database.rds.service.emoji.EmojiBagService;
import com.red.circle.wallet.inner.error.WalletErrorCode;
import com.red.circle.wallet.inner.endpoint.wallet.WalletGoldClient;
import com.red.circle.wallet.inner.model.cmd.GoldReceiptCmd;
import com.red.circle.wallet.inner.model.dto.WalletReceiptResDTO;
import com.red.circle.wallet.inner.model.enums.GoldOrigin;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 购买表情.
 *
 * @author pengshigang on 2022/6/29
 */
@Component
@RequiredArgsConstructor
public class EmojiBuyExe {

  private final EmojiBagService emojiBagService;
  private final WalletGoldClient walletGoldClient;
  private final EmojiCacheService emojiCacheService;

  public void execute(AppIdLongCmd cmd) {

    // 获得平台所有表情包
    List<UserEmojiBackpackCache> caches = getEmojiBackpackCaches(cmd);
    ResponseAssert.notEmpty(CommonErrorCode.NOT_FOUND_RECORD_INFO, caches);

    caches = caches.stream().filter(cache -> Objects.equals(cache.getId(), cmd.getId()))
        .collect(Collectors.toList());
    ResponseAssert.notEmpty(CommonErrorCode.NOT_FOUND_RECORD_INFO, caches);

    UserEmojiBackpackCache buy = caches.get(0);
    ResponseAssert.isFalse(CommonErrorCode.NO_SUITABLE_CONTENT,
        BigDecimal.ZERO.compareTo(buy.getAmount()) >= 0);

    ResponseAssert.isTrue(WalletErrorCode.INSUFFICIENT_BALANCE,
        ResponseAssert.requiredSuccess(
            walletGoldClient.getBalance(cmd.requiredReqUserId())
        ).getDollarAmount().longValue() >= 0);

    ResponseAssert.isFalse(CommonErrorCode.REPEATED_SUBMISSION,
        emojiBagService.existEmoji(cmd.requiredReqUserId(), cmd.getId()));

    WalletReceiptResDTO walletReceipt = getWalletReceipt(cmd, buy);

    emojiBagService.add(cmd.requiredReqUserId(), buy.getId());

    emojiCacheService.removeUserEmojiBackpack(cmd.requiredReqUserId());

  }

  private WalletReceiptResDTO getWalletReceipt(AppIdLongCmd cmd, UserEmojiBackpackCache buy) {
    return ResponseAssert.requiredSuccess(
        walletGoldClient.changeBalance(GoldReceiptCmd.builder()
            .appExpenditure()
            .userId(cmd.requiredReqUserId())
            .eventId(buy.getId())
            .sysOrigin(cmd.requireReqSysOrigin())
            .origin(GoldOrigin.BUY_EMOJI)
            .amount(buy.getAmount())
            .build())
    );
  }

  private List<UserEmojiBackpackCache> getEmojiBackpackCaches(AppIdLongCmd cmd) {
    return emojiCacheService.listEmojiAll(cmd.requireReqSysOrigin());
  }

}
