package com.red.circle.other.app.command.room;

import cn.hutool.core.util.ObjectUtil;
import com.red.circle.common.business.core.ImageSizeConst;
import com.red.circle.common.business.core.SensitiveWordFilter;
import com.red.circle.common.business.core.constant.KeywordConstant;
import com.red.circle.common.business.core.enums.DataApprovalTypeEnum;
import com.red.circle.external.inner.endpoint.oss.OssServiceClient;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.convertor.live.RoomProfileAppConvertor;
import com.red.circle.other.app.dto.cmd.room.UpdateRoomCmdData;
import com.red.circle.other.domain.gateway.approval.ProfileApprovalGateway;
import com.red.circle.other.domain.model.approval.ProfileApprovalContent;
import com.red.circle.other.infra.database.mongo.entity.live.RoomProfile;
import com.red.circle.other.infra.database.mongo.entity.sys.ApiOperationLog;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.infra.database.mongo.service.sys.ApiOperationLogService;
import com.red.circle.other.inner.asserts.DynamicErrorCode;
import com.red.circle.other.inner.asserts.RoomErrorCode;
import com.red.circle.other.inner.model.dto.live.RoomProfileDTO;
import com.red.circle.tool.core.date.DateUtils;
import com.red.circle.tool.core.json.JacksonUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 修改房间资料.
 *
 * @author houyong on 2020/12/4
 */
@Component
@RequiredArgsConstructor
public class UpdateRoomProfileCmdExe {

  private final OssServiceClient ossServiceClient;
  private final ProfileApprovalGateway profileApprovalGateway;
  private final ApiOperationLogService apiOperationLogService;
  private final RoomProfileAppConvertor roomProfileAppConvertor;
  private final RoomProfileManagerService roomProfileManagerService;

  public RoomProfileDTO execute(UpdateRoomCmdData cmd) {

    apiOperationLogService.add(
        new ApiOperationLog()
            .setId(IdWorkerUtils.getIdStr())
            .setRequestUserId(cmd.requiredReqUserId())
            .setApiDesc("房间资料变更")
            .setBusinessCode("ROOM_PROFILE_UPDATE")
            .setBusinessContent(Objects.toString(cmd.getId()))
            .setCmd(JacksonUtils.toJson(cmd))
            .setExpiredTime(DateUtils.nowPlusDays(7))
            .setCreateTime(DateUtils.now())
    );

    RoomProfile roomProfile = roomProfileManagerService.getProfileById(cmd.getId());
    ResponseAssert.notNull(RoomErrorCode.ROOM_NOT_EXISTS, roomProfile);
    ResponseAssert.isTrue(CommonErrorCode.INSUFFICIENT_PERMISSION,
        cmd.reqIsSelf(roomProfile.getUserId()));

    //违禁词
    if(ObjectUtil.isNotEmpty(cmd.getRoomName())){
      ResponseAssert.isFalse(DynamicErrorCode.SENSITIVE_WORD_ERROR, SensitiveWordFilter.checkSensitiveWord(cmd.getRoomName()));
    }
    if(ObjectUtil.isNotEmpty(cmd.getRoomDesc())){
      ResponseAssert.isFalse(DynamicErrorCode.SENSITIVE_WORD_ERROR, SensitiveWordFilter.checkSensitiveWord(cmd.getRoomDesc()));
    }
    cmd.setRoomName(KeywordConstant.filter(cmd.getRoomName()));
    cmd.setRoomDesc(KeywordConstant.filter(cmd.getRoomDesc()));
    if (StringUtils.isNotBlank(cmd.getRoomCover())) {
      cmd.setRoomCover(ossServiceClient.processImgSaveAsCompressZoom(cmd.getRoomCover(),
          ImageSizeConst.COVER_HEIGHT).getBody()
      );
    }

    RoomProfile updateRoomProfile = roomProfileAppConvertor.toRoomProfile(cmd);
    updateRoomProfile.setUserId(roomProfile.getUserId());
    updateRoomProfile.setSysOrigin(cmd.requireReqSysOrigin());
    roomProfileManagerService.updateSelectiveProfile(updateRoomProfile);

    submitApproval(updateRoomProfile);
    return roomProfileAppConvertor.toRoomProfileDTO(cmd);
  }

  private void submitApproval(RoomProfile updateRoomProfile) {
    if (StringUtils.isNotBlank(updateRoomProfile.getRoomCover())) {
      profileApprovalGateway.submitApproval(
          new ProfileApprovalContent()
              .setBeUserId(updateRoomProfile.getUserId())
              .setSysOrigin(updateRoomProfile.getSysOrigin())
              .setApprovalType(DataApprovalTypeEnum.ROOM_AVATAR)
              .addContent(
                  updateRoomProfile.getId(),
                  updateRoomProfile.getRoomCover()
              )
      );
    }

    if (StringUtils.isNotBlank(updateRoomProfile.getRoomName())) {
      profileApprovalGateway.submitApproval(
          new ProfileApprovalContent()
              .setBeUserId(updateRoomProfile.getUserId())
              .setSysOrigin(updateRoomProfile.getSysOrigin())
              .setApprovalType(DataApprovalTypeEnum.ROOM_NICKNAME)
              .addContent(
                  updateRoomProfile.getId(),
                  updateRoomProfile.getRoomName()
              )
      );
    }

    if (StringUtils.isNotBlank(updateRoomProfile.getRoomDesc())) {
      profileApprovalGateway.submitApproval(
          new ProfileApprovalContent()
              .setBeUserId(updateRoomProfile.getUserId())
              .setSysOrigin(updateRoomProfile.getSysOrigin())
              .setApprovalType(DataApprovalTypeEnum.ROOM_NOTICE)
              .addContent(
                  updateRoomProfile.getId(),
                  updateRoomProfile.getRoomDesc()
              )
      );
    }

  }

}
