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

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.game.GameLuckyBoxRewardCO;
import com.red.circle.other.app.dto.clientobject.game.GameLuckyUserListCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.mongo.entity.game.luck.GameLuckyBoxDraw;
import com.red.circle.other.infra.database.mongo.entity.game.luck.query.GameLuckyBoxQuery;
import com.red.circle.other.infra.database.mongo.service.game.GameLuckyBoxDrawService;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 最近游戏记录.
 *
 * @author pengliang on 2022/8/2
 */
@Component
@RequiredArgsConstructor
public class GameLuckyBoxGiftLatestQryExe {

  private final UserProfileGateway userProfileGateway;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final GameLuckyBoxDrawService gameLuckyBoxDrawService;

  public List<GameLuckyUserListCO> execute(AppExtCommand cmd) {

    GameLuckyBoxQuery query = new GameLuckyBoxQuery();
    query.setSysOrigin(cmd.requireReqSysOrigin());
    query.setLimit(20);
    List<GameLuckyBoxDraw> gameLuckyBoxDraws = Optional.ofNullable(
            gameLuckyBoxDrawService.list(query))
        .map(list -> {
          if (!Objects.equals(cmd.requireReqSysOrigin(), SysOriginPlatformEnum.ASWAT.name())) {
            return list;
          }
          GameLuckyBoxDraw boxDraw = gameLuckyBoxDrawService.getById(1562451610204880897L);
          if (Objects.isNull(boxDraw)) {
            return list;
          }

          List<GameLuckyBoxDraw> result = list.stream()
              .filter(box -> !Objects.equals(box.getId(), boxDraw.getId()))
              .collect(Collectors.toList());
          result.add(0, boxDraw);
          return result;
        }).orElse(null);

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

    Map<Long, UserProfileDTO> userProfileMap = userProfileAppConvertor.toMapUserProfileDTO(
        userProfileGateway.mapByUserIds(
            gameLuckyBoxDraws.stream().map(GameLuckyBoxDraw::getUserId)
                .collect(Collectors.toSet()))
    );

    return gameLuckyBoxDraws.stream().map(draws -> {
          UserProfileDTO userProfile = userProfileMap.get(draws.getUserId());
          return new GameLuckyUserListCO()
              .setUserId(userProfile.getId())
              .setUserAvatar(userProfile.getUserAvatar())
              .setUserNickname(userProfile.getUserNickname())
              .setGifts(draws.getLuckProps().stream()
                  .map(gameLuckyBoxProps -> new GameLuckyBoxRewardCO()
                      .setId(gameLuckyBoxProps.getPropsId())
                      .setGiftCandy(gameLuckyBoxProps.getAmount())
                      .setGiftName(gameLuckyBoxProps.getPropsName())
                      .setGiftPhoto(gameLuckyBoxProps.getPropsCover())
                      .setQuantity(gameLuckyBoxProps.getQuantity())
                  ).toList()
              );
        }
    ).toList();
  }
}
