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

import com.google.common.collect.Lists;
import com.red.circle.common.business.core.level.LevelUtils;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.common.business.dto.cmd.app.AppUserIdCmd;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.DateUtils;
import com.red.circle.tool.core.num.NumUtils;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.user.relation.friendcard.UserFriendCardCO;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.cache.service.user.UserFriendshipCardConfigCacheService;
import com.red.circle.other.infra.database.rds.entity.user.user.FriendshipCard;
import com.red.circle.other.infra.database.rds.entity.user.user.FriendshipCardConfig;
import com.red.circle.other.infra.database.rds.service.user.user.FriendshipCardService;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 获得自己的友谊关系卡.
 * </p>
 *
 * @author pengshigang
 * @since 2022-06-10
 */
@Component
@RequiredArgsConstructor
public class UserFriendCardsByUserIdQryExe {

  private final UserProfileGateway userProfileGateway;
  private final FriendshipCardService friendshipCardService;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final UserFriendshipCardConfigCacheService userFriendshipCardConfigCacheService;

  public List<UserFriendCardCO> execute(AppUserIdCmd cmd) {

    List<FriendshipCardConfig> cardConfigCaches = getCardConfigCaches(cmd);
    if (CollectionUtils.isEmpty(cardConfigCaches)) {
      return Lists.newArrayList();
    }

    return cardConfigCaches.stream().map(cardConfig -> {

      FriendshipCard friendshipCard = getByTypeByUserId(cmd, cardConfig);

      UserFriendCardCO cardCO = new UserFriendCardCO();

      cardCO.setCardType(cardConfig.getCardType());
      cardCO.setCardBack(cardConfig.getCardBack());
      cardCO.setBuyCardMoney(cardConfig.getCardMoney());

      if (Objects.isNull(friendshipCard)) {
        return cardCO;
      }

      // 获得好友等级
      cardCO.setCardLevel(
          LevelUtils.getUserCardLevel(cmd.requireReqSysOriginEnum(),
                  friendshipCard.getFriendshipValue().longValue())
              .getLevel());

      cardCO.setDays(DateUtils.toDurationDays(friendshipCard.getCreateTime(), DateUtils.now()));

      UserProfile userProfile = userProfileGateway.getByUserId(friendshipCard.getSendUserId());
      if (Objects.isNull(userProfile)) {
        return cardCO;
      }
      cardCO.setFriendshipValue(friendshipCard.getFriendshipValue());
      cardCO.setStrFriendshipValue(NumUtils.formatBigDecimal(friendshipCard.getFriendshipValue()));
      cardCO.setUserProfile(userProfileAppConvertor.toUserProfileDTO(userProfile));
      return cardCO;

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

  }

  private FriendshipCard getByTypeByUserId(AppUserIdCmd cmd,
      FriendshipCardConfig cardConfig) {
    return friendshipCardService
        .getMaxUserByTypeByUserId(cardConfig.getCardType(), cmd.getUserId());
  }

  private List<FriendshipCardConfig> getCardConfigCaches(AppExtCommand cmd) {
    return userFriendshipCardConfigCacheService.listSysOriginConfig(cmd.requireReqSysOrigin());
  }

}
