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

import com.red.circle.common.business.core.enums.ApprovalStatusEnum;
import com.red.circle.component.redis.service.RedisService;
import com.red.circle.external.inner.endpoint.message.OfficialNoticeClient;
import com.red.circle.external.inner.endpoint.oss.OssServiceClient;
import com.red.circle.external.inner.model.enums.message.OfficialNoticeTypeEnum;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.dto.cmd.room.RoomThemeCustomizeCmd;
import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService;
import com.red.circle.other.infra.database.rds.entity.props.RoomThemeUserCustomize;
import com.red.circle.other.infra.database.rds.service.props.RoomThemeUserCustomizeService;
import com.red.circle.other.inner.enums.config.EnumConfigKey;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 自定义主题
 *
 * @author pengliang on 2021/1/23
 */
@Component
@RequiredArgsConstructor
public class RoomThemeUseCustomizeThemeCmdExe {

  private final RedisService redisService;
  private final OssServiceClient ossServiceClient;
  private final WalletGoldClient walletGoldClient;
  private final OfficialNoticeClient officialNoticeClient;
  private final EnumConfigCacheService enumConfigCacheService;
  private final RoomThemeUserCustomizeService roomThemeUserCustomizeService;

  public BigDecimal execute(RoomThemeCustomizeCmd cmd) {
    String key = "CUSTOMIZE:" + cmd.requiredReqUserId();
    ResponseAssert.isTrue(CommonErrorCode.REQUEST_LIMITING, redisService.lock(key, 60));
    RoomThemeUserCustomize customizeRoomUserTheme = new RoomThemeUserCustomize()
        .setId(IdWorkerUtils.getId())
        .setSysOrigin(cmd.requireReqSysOrigin())
        .setUserId(cmd.requiredReqUserId())
        .setThemeBack(
            ResponseAssert.requiredSuccess(ossServiceClient.getAccessUrl(cmd.getThemeUrl())))
        .setThemeMoney(
            enumConfigCacheService.getValueBigDecimal(EnumConfigKey.CUSTOM_ROOM_THEME,
                cmd.requireReqSysOrigin())
        )
        .setThemeStatus(ApprovalStatusEnum.PENDING.name());

    WalletReceiptResDTO receiptRes = ResponseAssert.requiredSuccess(
        walletGoldClient.changeBalance(GoldReceiptCmd.builder()
            .appExpenditure()
            .userId(cmd.requiredReqUserId())
            .eventId(customizeRoomUserTheme.getId())
            .sysOrigin(cmd.requireReqSysOrigin())
            .origin(GoldOrigin.CUSTOM_ROOM_THEME)
            .amount(customizeRoomUserTheme.getThemeMoney())
            .build())
    );

    if (roomThemeUserCustomizeService.save(customizeRoomUserTheme)) {
//      officialNoticeClient.send(cmd.requiredReqUserId(), OfficialNoticeTypeEnum.ROOM_THEME_PENDING);
    }

    redisService.unlock(key);
    return receiptRes.getBalance().getDollarAmount();
  }

}
