package com.red.circle.other.app.inner.service.material.gift.impl;

import com.google.common.collect.Lists;
import com.red.circle.other.app.inner.convertor.material.GiftInnerConvertor;
import com.red.circle.other.app.inner.service.material.gift.GiftGiveRunningWaterClientService;
import com.red.circle.other.infra.database.mongo.entity.gift.GiftGiveRunningWater;
import com.red.circle.other.infra.database.mongo.entity.live.RoomProfile;
import com.red.circle.other.infra.database.mongo.service.gift.GiftGiveRunningWaterService;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.infra.database.rds.entity.gift.GiftConfig;
import com.red.circle.other.infra.database.rds.service.gift.GiftConfigService;
import com.red.circle.other.inner.model.cmd.gift.GiftGiveRunningWaterBackQryCmd;
import com.red.circle.other.inner.model.cmd.gift.SumAnchorTargetQryCmd;
import com.red.circle.other.inner.model.dto.gift.GiftGiveRunningWaterDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.regex.RegexConstant;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 礼物流水.
 *
 * @author pengliang on 2023/8/19
 */
@Service
@RequiredArgsConstructor
public class GiftGiveRunningWaterClientServiceImpl implements GiftGiveRunningWaterClientService {

  private final GiftConfigService giftConfigService;
  private final GiftInnerConvertor giftInnerConvertor;
  private final RoomProfileManagerService roomProfileManagerService;
  private final GiftGiveRunningWaterService giftGiveRunningWaterService;

  @Override
  public void updateGiftStatusRefunded(Long sendUserId, Integer startDateNumber,
      Integer endDateNumber) {
    giftGiveRunningWaterService.updateGiftStatusRefunded(sendUserId, startDateNumber,
        endDateNumber);
  }

  @Override
  public List<GiftGiveRunningWaterDTO> listGiftGiveRunningWater(
      GiftGiveRunningWaterBackQryCmd query) {
    if (Objects.nonNull(query.getAcceptUserId()) && query.getAcceptUserId() <= 0) {
      return Lists.newArrayList();
    }
    if (Objects.nonNull(query.getSendUserId()) && query.getSendUserId() <= 0) {
      return Lists.newArrayList();
    }
    return assembleVo(giftGiveRunningWaterService.list(
        giftInnerConvertor.toGiftGiveRunningWater(query)));
  }

  private List<GiftGiveRunningWaterDTO> assembleVo(
      List<GiftGiveRunningWater> runningWaters) {
    if (CollectionUtils.isEmpty(runningWaters)) {
      return Lists.newArrayList();
    }
    Map<Long, RoomProfile> roomProfileV2Map = roomProfileManagerService.mapProfileByRoomIds(
        runningWaters.stream().map(GiftGiveRunningWater::getOriginId)
            .filter(originId -> originId.matches(RegexConstant.NUMBER))
            .map(Long::parseLong)
            .collect(Collectors.toSet()));

    Map<Long, GiftConfig> giftConfigMap = giftConfigService.mapByIds(
        runningWaters.stream().map(GiftGiveRunningWater::getGiftId).collect(Collectors.toSet()));

    return runningWaters.stream().map(runningWater -> {
      GiftGiveRunningWaterDTO vo = giftInnerConvertor.toGiftGiveRunningWaterDTO(runningWater);

      if (vo.getOriginId().matches(RegexConstant.NUMBER)) {
        vo.setRoomProfile(
            giftInnerConvertor.toRoomProfileDTO(
                roomProfileV2Map.get(Long.parseLong(vo.getOriginId()))));
      }

      GiftConfig giftConfig = giftConfigMap.get(runningWater.getGiftId());
      vo.setGiftCover(Objects.nonNull(giftConfig) ? giftConfig.getGiftPhoto() : "");
      return vo;
    }).collect(Collectors.toList());
  }

  @Override
  public String sumGiftQuantity(GiftGiveRunningWaterBackQryCmd query) {
    return giftGiveRunningWaterService.sumGiftQuantity(
        giftInnerConvertor.toGiftGiveRunningWater(query));
  }

  @Override
  public String sumGiftValue(GiftGiveRunningWaterBackQryCmd query) {
    return giftGiveRunningWaterService.sumGiftValue(
        giftInnerConvertor.toGiftGiveRunningWater(query));
  }


  @Override
  public BigDecimal sumAnchorTarget(SumAnchorTargetQryCmd query) {
    return giftGiveRunningWaterService.sumAnchorTarget(query);
  }

  @Override
  public BigDecimal sumAnchorTargetLuckyGift(SumAnchorTargetQryCmd query) {
    return giftGiveRunningWaterService.sumAnchorTargetLuckyGift(query);
  }
}
