package com.red.circle.other.app.command.game.luckybox;


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.red.circle.common.business.dto.cmd.LastIdCmd;
import com.red.circle.other.app.convertor.game.GameLuckyBoxConvertor;
import com.red.circle.other.app.dto.clientobject.game.GameLuckyBoxCountCO;
import com.red.circle.other.app.dto.clientobject.game.GameLuckyBoxCountDetailsCO;
import com.red.circle.other.infra.database.rds.entity.game.GameLuckyBoxCount;
import com.red.circle.other.infra.database.rds.entity.game.GameLuckyBoxCountDetails;
import com.red.circle.other.infra.database.rds.entity.gift.GiftConfig;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxCountDetailsService;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxCountService;
import com.red.circle.other.infra.database.rds.service.gift.GiftConfigService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * luckyBox抽奖记录
 *
 * @author zongpubin on 2023/8/28
 */

@Component
@RequiredArgsConstructor
public class GameLuckyBoxCountQryExe {

  private final GiftConfigService giftConfigService;
  private final GameLuckyBoxConvertor gameLuckyBoxConvertor;
  private final GameLuckyBoxCountService gameLuckyBoxCountService;
  private final GameLuckyBoxCountDetailsService gameLuckyBoxCountDetailsService;

  public List<GameLuckyBoxCountCO> execute(LastIdCmd cmd) {

    List<GameLuckyBoxCount> gameLuckyBoxCounts = gameLuckyBoxCountService.listByUserId(
        cmd.requiredReqUserId(), cmd.getLastId());

    if (CollectionUtils.isEmpty(gameLuckyBoxCounts)) {
      return Lists.newArrayList();
    }

    List<GameLuckyBoxCountDetails> countDetails = gameLuckyBoxCountDetailsService.getCountDetails(
        gameLuckyBoxCounts.stream().map(GameLuckyBoxCount::getId).collect(
            Collectors.toSet()));

    if (CollectionUtils.isEmpty(countDetails)) {
      return Lists.newArrayList();
    }

    Map<Long, GiftConfig> longGiftConfigMap = giftConfigService.mapByIds(
        countDetails.stream().map(GameLuckyBoxCountDetails::getGiftId).collect(
            Collectors.toSet()));

    Map<Long, List<GameLuckyBoxCountDetailsCO>> countDetailsMap = Optional.ofNullable(countDetails)
        .map(countDetailsInfos -> countDetailsInfos.stream().map(countDetailsInfo -> {
          GameLuckyBoxCountDetailsCO gameLuckyBoxCountDetailsCO = gameLuckyBoxConvertor.toCountDetailsCO(
              countDetailsInfo);
          gameLuckyBoxCountDetailsCO.setPhotoUrl(
              longGiftConfigMap.get(gameLuckyBoxCountDetailsCO.getGiftId()).getGiftPhoto());
          return gameLuckyBoxCountDetailsCO;
        }).collect(Collectors.groupingBy(GameLuckyBoxCountDetailsCO::getCountId)))
        .orElse(Maps.newHashMap());

    return gameLuckyBoxCounts.stream().map(gameLuckyBoxCount -> {
      GameLuckyBoxCountCO gameLuckyBoxCountCO = gameLuckyBoxConvertor.toLuckyBoxCountCO(
          gameLuckyBoxCount);
      gameLuckyBoxCountCO.setGameLuckyBoxCountDetails(
          countDetailsMap.get(gameLuckyBoxCountCO.getId()));
      return gameLuckyBoxCountCO;
    }).collect(Collectors.toList());
  }
}
