package com.red.circle.console.app.service.app.game;

import com.red.circle.console.app.convertor.sys.GameLuckyGiftConfigAppConvertor;
import com.red.circle.console.app.dto.clienobject.game.GameLuckyGiftCO;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.inner.endpoint.game.GameLuckyGiftConfigClient;
import com.red.circle.other.inner.endpoint.live.RoomManagerClient;
import com.red.circle.other.inner.model.cmd.game.GameLuckGiftProbabilityConfigQryCmd;
import com.red.circle.other.inner.model.cmd.game.GameLuckGiftRuleConfigQryCmd;
import com.red.circle.other.inner.model.cmd.game.GameLuckGiftStandardConfigQryCmd;
import com.red.circle.other.inner.model.cmd.game.LuckGiftGameCountQryCmd;
import com.red.circle.other.inner.model.cmd.game.LuckGiftGameQryCmd;
import com.red.circle.other.inner.model.dto.game.GameLuckyGiftCountDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyGiftDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyGiftProbabilityDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyGiftProbabilityInfoDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyGiftRuleConfigDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyGiftStandardConfigDTO;
import com.red.circle.other.inner.model.dto.game.LuckyGiftStandardMapDTO;
import com.red.circle.other.inner.model.dto.live.RoomProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import java.util.Collection;
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 zongpubin on 2024/1/24
 */
@Service
@RequiredArgsConstructor
public class GameLuckyGiftConfigServiceImpl implements GameLuckyGiftConfigService {

  private final UserProfileClient userProfileClient;
  private final RoomManagerClient roomManagerClient;
  private final GameLuckyGiftConfigClient gameLuckyGiftConfigClient;
  private final GameLuckyGiftConfigAppConvertor gameLuckyGiftConfigAppConvertor;


  @Override
  public PageResult<GameLuckyGiftCO> pageLuckyGiftGameRecord(LuckGiftGameQryCmd query) {
    PageResult<GameLuckyGiftDTO> pageResult = ResponseAssert.requiredSuccess(
        gameLuckyGiftConfigClient.pageLuckyGiftGameRecord(query));
    if (CollectionUtils.isEmpty(pageResult.getRecords())) {
      return pageResult.convert(gameLuckyGiftConfigAppConvertor::toGameLuckyGiftCO);
    }

    Map<Long, UserProfileDTO> userProfileMap = getUserProfileMap(pageResult.getRecords());
    Map<Long, RoomProfileDTO> roomProfileMap = ResponseAssert.requiredSuccess(
        roomManagerClient.mapProfileByRoomIds(
            pageResult.getRecords().stream().map(GameLuckyGiftDTO::getRoomId).collect(
                Collectors.toSet())));

    var result =  pageResult.convert(gameLuckyGiftDTO -> {
      UserProfileDTO userProfile = userProfileMap.get(gameLuckyGiftDTO.getUserId());
      RoomProfileDTO roomProfile = roomProfileMap.get(gameLuckyGiftDTO.getRoomId());
      if (Objects.isNull(roomProfile) || Objects.isNull(userProfile)) {
        return null;
      }
      GameLuckyGiftCO giftCount = gameLuckyGiftConfigAppConvertor.toGameLuckyGiftCO(
          gameLuckyGiftDTO);
      giftCount.setRoomProfile(roomProfile);
      giftCount.setUserProfile(userProfile);
      return giftCount;
    });
    result.setRecords(result.getRecords().stream().filter(Objects::nonNull).collect(
            Collectors.toList()));
    if (result.getRecords().size() == 0){
      return new PageResult<>();
    }
      return  result;
  }

  private Map<Long, UserProfileDTO> getUserProfileMap(Collection<GameLuckyGiftDTO> records) {
    return ResponseAssert.requiredSuccess(userProfileClient.mapByUserIds(
        records.stream().map(GameLuckyGiftDTO::getUserId)
            .collect(Collectors.toSet())));
  }

  @Override
  public GameLuckyGiftCountDTO countLuckyGiftGame(LuckGiftGameCountQryCmd query) {
    return ResponseAssert.requiredSuccess(
        gameLuckyGiftConfigClient.countLuckyGiftGame(query));
  }

  @Override
  public List<LuckyGiftStandardMapDTO> listProbability(String sysOrigin) {
    return ResponseAssert.requiredSuccess(
        gameLuckyGiftConfigClient.listProbability(sysOrigin));
  }

  @Override
  public GameLuckyGiftRuleConfigDTO getLuckyGiftRuleConfig(GameLuckGiftRuleConfigQryCmd query) {
    return ResponseAssert.requiredSuccess(
        gameLuckyGiftConfigClient.getLuckyGiftRuleConfig(query));
  }

  @Override
  public void addLuckyGiftRuleConfig(GameLuckyGiftRuleConfigDTO param) {
    gameLuckyGiftConfigClient.addLuckyGiftRuleConfig(param);
  }

  @Override
  public PageResult<GameLuckyGiftStandardConfigDTO> getStandardConfig(
      GameLuckGiftStandardConfigQryCmd query) {
    return ResponseAssert.requiredSuccess(
        gameLuckyGiftConfigClient.getStandardConfig(query));
  }

  @Override
  public void addLuckyGiftStandardConfig(GameLuckyGiftStandardConfigDTO param) {
    gameLuckyGiftConfigClient.addLuckyGiftStandardConfig(param);
  }

  @Override
  public void deleteStandardConfig(Long id, Long userId) {
    gameLuckyGiftConfigClient.deleteStandardConfig(id, userId);
  }

  @Override
  public List<GameLuckyGiftProbabilityDTO> getLuckyGiftProbabilityConfig(
      GameLuckGiftProbabilityConfigQryCmd query) {
    return ResponseAssert.requiredSuccess(
        gameLuckyGiftConfigClient.getLuckyGiftProbabilityConfig(query));
  }

  @Override
  public void addLuckyGiftProbabilityInfoConfig(GameLuckyGiftProbabilityInfoDTO param) {
    gameLuckyGiftConfigClient.addLuckyGiftProbabilityInfoConfig(param);
  }
}
