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

import com.google.common.base.Throwables;
import com.red.circle.common.business.core.enums.DataApprovalTypeEnum;
import com.red.circle.component.mq.MessageEventProcess;
import com.red.circle.component.mq.MessageEventProcessDescribe;
import com.red.circle.component.mq.config.RocketMqMessageListener;
import com.red.circle.component.mq.service.Action;
import com.red.circle.component.mq.service.ConsumerMessage;
import com.red.circle.component.mq.service.MessageListener;
import com.red.circle.mq.business.model.event.approval.CensorContent;
import com.red.circle.mq.business.model.event.approval.CensorProfileEvent;
import com.red.circle.mq.business.model.event.room.RoomVoiceCreateSuccessEvent;
import com.red.circle.mq.rocket.business.producer.CensorMqMessage;
import com.red.circle.mq.rocket.business.streams.CreateRoomSuccessSink;
import com.red.circle.other.infra.database.rds.service.approval.ApprovalUserSettingDataService;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author pengliang on 2020/12/18
 */
@Slf4j
@RocketMqMessageListener(groupId = CreateRoomSuccessSink.INPUT, tag = CreateRoomSuccessSink.TAG)
@RequiredArgsConstructor
public class CreateRoomSuccessListener implements MessageListener {

  private final CensorMqMessage otherMqMessage;
  private final MessageEventProcess messageEventProcess;
  private final ApprovalUserSettingDataService approvalUserSettingDataService;

  @Override
  public Action consume(ConsumerMessage message) {
    return messageEventProcess.consume(
        MessageEventProcessDescribe.builder()
            .logTag("创建房间")
            .consumeMsgTimeoutMinute(60)
            .repeatConsumeMinute(0)
            .repeatConsumeTag("CreateRoomSuccess")
            .checkConsumeTag(CreateRoomSuccessSink.TAG)
            .message(message)
            .build(),
        RoomVoiceCreateSuccessEvent.class,
        this::execute);
  }

  private void execute(RoomVoiceCreateSuccessEvent cmd) {
    List<DataApprovalTypeEnum> approvalTypes = CollectionUtils.newArrayList();
    approvalTypes.add(DataApprovalTypeEnum.ROOM_NICKNAME);
    if (StringUtils.isNotBlank(cmd.getRoomCover()) && !cmd.getRoomCover().contains("default")) {
      approvalTypes.add(DataApprovalTypeEnum.ROOM_AVATAR);
      // 发送违规图片鉴定
      otherMqMessage.image(
          new CensorProfileEvent()
              .setUserId(cmd.getUserId())
              .setApprovalType(DataApprovalTypeEnum.ROOM_AVATAR)
              .addContent(new CensorContent()
                  .setId(cmd.getUserId())
                  .setContent(cmd.getRoomCover())
              )
      );
    }

    try {
      approvalUserSettingDataService.saveBatchApproval(cmd.getUserId(), cmd.getSysOrigin(), approvalTypes);
    } catch (Exception ex) {
      log.error("{}", Throwables.getStackTraceAsString(ex));
    }

  }
}
