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

import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.app.convertor.material.PropsRedPacketSkinAppConvertor;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.game.GameRedPacketSenderUserCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.mongo.entity.game.redpacket.GameRedPacket;
import com.red.circle.other.infra.database.mongo.service.game.GameRedPacketSendRecordService;
import com.red.circle.other.infra.database.rds.entity.props.PropsBackpack;
import com.red.circle.other.infra.database.rds.entity.props.PropsRedPacketSkin;
import com.red.circle.other.infra.database.rds.service.props.PropsBackpackService;
import com.red.circle.other.infra.database.rds.service.props.PropsRedPacketSkinService;
import com.red.circle.other.inner.asserts.GameErrorCode;
import com.red.circle.other.inner.enums.material.PropsTypeEnum;
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.Objects;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 获取红包发件人信息.
 *
 * @author pengliang on 2021/5/17
 */
@Component
@RequiredArgsConstructor
public class GameRedPacketSenderUserQryExe {

  private final UserProfileGateway userProfileGateway;
  private final PropsBackpackService propsBackpackService;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final PropsRedPacketSkinService propsRedPacketSkinService;
  private final PropsRedPacketSkinAppConvertor propsRedPacketSkinAppConvertor;
  private final GameRedPacketSendRecordService gameRedPacketSendRecordService;

  public GameRedPacketSenderUserCO execute(AppIdLongCmd cmd) {
    GameRedPacket gameRedPacket = gameRedPacketSendRecordService.getById(cmd.getId());
    // 8000 没有找到红包信息
    ResponseAssert.notNull(GameErrorCode.NOT_FOUND_RED_PACKET, gameRedPacket);
    UserProfileDTO userProfileDTO = userProfileAppConvertor.toUserProfileDTO(
        userProfileGateway.getByUserId(gameRedPacket.getUserId()));

    GameRedPacketSenderUserCO senderUserCO = new GameRedPacketSenderUserCO();
    senderUserCO.setUserProfile(userProfileDTO);
    senderUserCO.setRedPacketProps(
        propsRedPacketSkinAppConvertor.toPropsRedPacketSkinDTO(
            getRedPacketProps(userProfileDTO))
    );
    return senderUserCO;
  }

  private PropsRedPacketSkin getRedPacketProps(UserProfileDTO userProfile) {

    List<PropsBackpack> propsUserBackpacks = Optional.ofNullable(
            propsBackpackService.listNotExpiredUseProps(userProfile.getId())
        )
        .map(list -> list.stream().filter(
                v -> v.getUseProps() && Objects.equals(v.getType(), PropsTypeEnum.RED_PACKET.name()))
            .toList()
        )
        .orElseGet(CollectionUtils::newArrayList);

    if (CollectionUtils.isEmpty(propsUserBackpacks)) {
      return null;
    }

    PropsRedPacketSkin redPacketSkin =
        propsRedPacketSkinService.getById(propsUserBackpacks.get(0).getPropsId()
        );
    if (Objects.isNull(redPacketSkin)) {
      return null;
    }

    return redPacketSkin;
  }

}
