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

import com.red.circle.console.app.dto.clienobject.game.GameTrumpetRecordCO;
import com.red.circle.console.infra.database.rds.service.admin.UserService;
import com.red.circle.other.inner.endpoint.game.GameTrumpetCountClient;
import com.red.circle.other.inner.model.cmd.game.GameTrumpetQryCmd;
import com.red.circle.other.inner.model.dto.game.GameTrumpetCountDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
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/12/23
 */
@Service
@RequiredArgsConstructor
public class GameTrumpetServiceImpl implements GameTrumpetService {

  private final UserService userService;
  private final UserProfileClient userProfileClient;
  private final GameTrumpetCountClient gameTrumpetCountClient;

  @Override
  public List<GameTrumpetRecordCO> list(GameTrumpetQryCmd query) {
    List<GameTrumpetCountDTO> trumpetCounts = gameTrumpetCountClient.opsList(query).getBody();

    if (CollectionUtils.isEmpty(trumpetCounts)) {
      return CollectionUtils.newArrayList();
    }

    Map<Long, UserProfileDTO> profileMap = userProfileClient.mapByUserIds(
        trumpetCounts.stream().map(GameTrumpetCountDTO::getUserId).collect(Collectors.toSet())
    ).getBody();

    if (CollectionUtils.isEmpty(profileMap)) {
      return CollectionUtils.newArrayList();
    }

    Map<Long, String> opsUserNicknameMap = getOpsUpdateUserNickname(trumpetCounts);

    return trumpetCounts.stream().map(trumpet -> {

      if (Objects.isNull(trumpet)) {
        return null;
      }

      UserProfileDTO userProfile = profileMap.get(trumpet.getUserId());
      if (Objects.isNull(userProfile)) {
        return null;
      }

      GameTrumpetRecordCO dto = new GameTrumpetRecordCO()
          .setId(trumpet.getId())
          .setMessage(trumpet.getMessage())
          .setAmount(trumpet.getAmount())
          .setRoomId(trumpet.getRoomId())
          .setCreateTime(trumpet.getCreateTime())
          .setUserId(trumpet.getUserId())
          .setSysOrigin(trumpet.getSysOrigin())
          .setUpdateTime(trumpet.getUpdateTime())
          .setUserProfile(userProfile);

      if (Objects.nonNull(trumpet.getUpdateUser())) {
        dto.setOpsUpdateUserNickname(opsUserNicknameMap.get(trumpet.getUpdateUser()));
      }

      return dto;

    }).filter(Objects::nonNull).collect(Collectors.toList());

  }

  private Map<Long, String> getOpsUpdateUserNickname(List<GameTrumpetCountDTO> trumpetCounts) {
    return userService.mapBackUserNickname(
        trumpetCounts.stream().map(GameTrumpetCountDTO::getUpdateUser)
            .collect(Collectors.toSet()));
  }

  @Override
  public void delete(Long id, Long updateUser) {
    gameTrumpetCountClient.delete(id, updateUser);
  }

}
