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

import com.google.common.collect.Lists;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.app.common.game.GameLuckyBoxCommon;
import com.red.circle.other.app.dto.clientobject.game.GameLuckyBoxAwardConfigCO;
import com.red.circle.other.app.dto.clientobject.game.GameLuckyBoxAwardDetailsConfigCO;
import com.red.circle.other.infra.database.rds.entity.game.GameLuckyBoxAwardConfig;
import com.red.circle.other.infra.database.rds.entity.game.GameLuckyBoxUserOpenedAward;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxAwardConfigService;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxUserOpenedAwardService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author zongpubin on 2023/8/28
 */

@Component
@RequiredArgsConstructor
public class GameLuckyBoxAwardConfigQryExe {

  private final GameLuckyBoxCommon gameLuckyBoxCommon;
  private final GameLuckyBoxAwardConfigService gameLuckyBoxAwardConfigService;
  private final GameLuckyBoxUserOpenedAwardService gameLuckyBoxUserOpenedAwardService;

  public List<GameLuckyBoxAwardConfigCO> execute(AppExtCommand cmd) {

    List<GameLuckyBoxAwardConfig> gameLuckyBoxAwardConfigs = gameLuckyBoxAwardConfigService.listBySysOrigin(
        cmd.getReqSysOrigin().getOrigin());

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

    Map<Long, GameLuckyBoxUserOpenedAward> openedAwardMap = gameLuckyBoxUserOpenedAwardService.getByUserId(
        cmd.getReqUserId());

    return gameLuckyBoxAwardConfigs.stream()
        .map(gameLuckyBoxAwardConfig -> {
          List<GameLuckyBoxAwardDetailsConfigCO> awardDetailsInfos = getAwardDetailsInfo(
              gameLuckyBoxAwardConfig.getId(),
              gameLuckyBoxAwardConfig.getLotteryType());

          GameLuckyBoxAwardConfigCO awardConfigCO = new GameLuckyBoxAwardConfigCO()
              .setId(gameLuckyBoxAwardConfig.getId())
              .setName(gameLuckyBoxAwardConfig.getName())
              .setAwardDetailsConfigs(awardDetailsInfos)
              .setLotteryType(gameLuckyBoxAwardConfig.getLotteryType())
              .setSysOrigin(gameLuckyBoxAwardConfig.getSysOrigin());

          if (CollectionUtils.isNotEmpty(awardDetailsInfos)) {
            awardDetailsInfos.forEach(awardDetailsInfo -> awardDetailsInfo.setIsOpened(
                Objects.nonNull(openedAwardMap.get(awardDetailsInfo.getId()))));
          }

          return awardConfigCO;
        })
        .collect(Collectors.toList());
  }

  private List<GameLuckyBoxAwardDetailsConfigCO> getAwardDetailsInfo(Long awardId,
      String lotteryType) {
    return gameLuckyBoxCommon.getAwardDetailsInfo(awardId, lotteryType);
  }
}
