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


import com.red.circle.external.inner.endpoint.message.ImGroupClient;
import com.red.circle.external.inner.model.cmd.message.GroupUnbanCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.app.dto.cmd.user.user.UserShieldBlackUserCmd;
import com.red.circle.other.infra.database.mongo.entity.live.RoomProfile;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.infra.database.mongo.service.live.RoomUserBlacklistService;
import com.red.circle.other.infra.database.rds.service.user.user.ShieldBlackService;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 用户取消屏蔽拉黑.
 *
 * @author pengliang on 2020/11/4
 */
@Component
@RequiredArgsConstructor
public class UserShieldBlackCancelCmdExe {

  private final ImGroupClient imGroupClient;
  private final ShieldBlackService shieldBlackService;
  private final RoomUserBlacklistService roomUserBlacklistService;
  private final RoomProfileManagerService roomProfileManagerService;

  public void execute(UserShieldBlackUserCmd cmd) {

    shieldBlackService.removeShieldBlack(cmd.getReqUserId(), cmd.getShieldUserId());
    RoomProfile roomProfile = roomProfileManagerService.getProfileByUserId(cmd.getReqUserId());

    if (Objects.nonNull(roomProfile)) {
      Boolean status = ResponseAssert.requiredSuccess(
          imGroupClient.unBanGroupMember(new GroupUnbanCmd()
              .setGroupId(Objects.toString(roomProfile.getRoomAccount()))
              .setUserId(cmd.getShieldUserId())
          ));

      if (Objects.equals(status, Boolean.TRUE)) {
        roomUserBlacklistService.removeBlack(roomProfile.getId(), cmd.getShieldUserId());
      }
    }

  }
}
