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

import com.google.common.collect.Lists;
import com.red.circle.common.business.enums.DynamicMessageEnum;
import com.red.circle.other.app.dto.clientobject.dynamic.DynamicMessageListCO;
import com.red.circle.other.app.dto.cmd.dynamic.DynamicLimitCmd;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicContent;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicMessage;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicContentService;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicMessageService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 动态-我的消息列表.
 * </p>
 *
 * @author pengshigang
 * @since 2022-04-13
 */
@Component
@RequiredArgsConstructor
public class DynamicMessageQryExe {

  private final UserProfileGateway userProfileGateway;
  private final DynamicContentService dynamicContentService;
  private final DynamicMessageService dynamicMessageService;

  public List<DynamicMessageListCO> execute(DynamicLimitCmd cmd) {

    List<DynamicMessage> messages = getMessages(cmd);
    if (CollectionUtils.isEmpty(messages)) {
      return Lists.newArrayList();
    }
    Map<Long, UserProfile> userMap = userProfileGateway.mapByUserIds(getUserIds(messages));
    Map<Long, DynamicContent> contentMap = getContentMap(messages);

    return messages.stream().map(message -> {

      DynamicContent dynamicContent = contentMap.get(message.getDynamicContentId());
      if (Objects.isNull(dynamicContent)) {
        return null;
      }

      DynamicMessageListCO messageCO = new DynamicMessageListCO();
      messageCO.setId(message.getId());
      messageCO.setType(message.getType());
      messageCO.setUserId(message.getCreateUser());
      messageCO.setCreateTime(message.getCreateTime());
      messageCO.setMessageContent(message.getContent());
      messageCO.setDynamicId(message.getDynamicContentId());
      messageCO.setDynamicContent(dynamicContent.getContent());

      if (DynamicMessageEnum.isReportRelated(message.getType())) {
        return messageCO;
      }

      UserProfile user = userMap.get(message.getCreateUser());
      if (Objects.isNull(user)) {
        return null;
      }
      messageCO.setUserAvatar(user.getUserAvatar());
      messageCO.setUserNickname(user.getUserNickname());
      return messageCO;

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

  }

  private Map<Long, DynamicContent> getContentMap(List<DynamicMessage> messages) {
    return dynamicContentService
        .mapByIds(messages.stream().map(DynamicMessage::getDynamicContentId).collect(
            Collectors.toSet()));
  }

  private List<DynamicMessage> getMessages(DynamicLimitCmd cmd) {
    return dynamicMessageService.pageByTimeDesc(cmd.getPageNumber(), cmd.getReqUserId());
  }

  private Set<Long> getUserIds(List<DynamicMessage> messages) {

    return messages.stream().map(DynamicMessage::getCreateUser)
        .filter(Objects::nonNull).collect(Collectors.toSet());
  }


}
