package com.red.circle.other.app.listener.strategy;

import com.red.circle.common.business.enums.DynamicMessageEnum;
import com.red.circle.external.inner.endpoint.message.NewsletterClient;
import com.red.circle.external.inner.model.cmd.message.NewsletterResultCmd;
import com.red.circle.external.inner.model.enums.message.NewsletterEvent;
import com.red.circle.mq.business.model.event.approval.ApprovalContent;
import com.red.circle.mq.business.model.event.approval.ApprovalEvent;
import com.red.circle.other.infra.database.mongo.service.dynamic.DynamicPopularService;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicContent;
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;

/**
 * 动态内容-审核
 *
 * @author pengshigang on 2022/4/20
 */
@Component("DYNAMIC_CONTENT_APPROVAL")
@RequiredArgsConstructor
public class ApprovalDynamicContentStrategy implements ApprovalStrategy {

  private final NewsletterClient newsletterClient;
  private final DynamicPopularService dynamicPopularService;
  private final DynamicMessageService dynamicMessageService;
  private final DynamicContentService dynamicContentService;

  @Override
  public void doOperation(ApprovalEvent param) {
    if (!param.approvalNotPass()) {
      return;
    }

    if (CollectionUtils.isEmpty(param.getWaitApprovalUser())) {
      return;
    }

    List<DynamicContent> contents = dynamicContentService.listByIds(getDynamicIds(param));
    if (CollectionUtils.isEmpty(contents)) {
      return;
    }

    Map<Long, DynamicContent> dynamicMap = getDynamicMap(contents);
    if (CollectionUtils.isEmpty(dynamicMap)) {
      return;
    }

    //保存消息与记录
    process(param, dynamicMap);

    //发送消息标记
    dynamicMessageService.mapByUserIdsByContentId(getUserIds(contents))
        .forEach((key, list) -> newsletterClient
            .send(new NewsletterResultCmd()
                .setUserIds(List.of(key))
                .setEvent(NewsletterEvent.DYNAMIC_MESSAGE_QUANTITY)
                .setData(list.size())
            )
        );

  }

  private Map<Long, DynamicContent> getDynamicMap(List<DynamicContent> contents) {
    return contents.stream()
        .collect(Collectors.toMap(DynamicContent::getId, v -> v));
  }

  private Set<Long> getDynamicIds(ApprovalEvent param) {
    return param.getWaitApprovalUser().stream().map(ApprovalContent::getContentId)
        .collect(Collectors.toSet());
  }

  private Set<Long> getUserIds(List<DynamicContent> contents) {
    return contents.parallelStream().map(DynamicContent::getCreateUser)
        .collect(Collectors.toSet());
  }

  private void process(ApprovalEvent param,
      Map<Long, DynamicContent> dynamicMap) {

    param.getWaitApprovalUser()
        .forEach(approval -> {

          DynamicContent dynamicContent = dynamicMap.get(approval.getContentId());
          if (Objects.isNull(dynamicContent)) {
            return;
          }

          //删除动态
          dynamicContentService.deleteById(dynamicContent.getId());

          dynamicPopularService.removeByDynamicId(dynamicContent.getId());

          //保存数据
          dynamicMessageService.save(dynamicContent.getContent(),
              dynamicContent.getId(),
              dynamicContent.getCreateUser(),
              DynamicMessageEnum.valueOf(approval.getTags()));
        });

  }

}
