package com.red.circle.other.app.inner.service.live.impl;

import com.red.circle.external.inner.endpoint.message.AgoraClient;
import com.red.circle.external.inner.endpoint.message.ImGroupClient;
import com.red.circle.external.inner.model.cmd.message.GroupBanCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.inner.convertor.live.RoomUserBlacklistInnerConvertor;
import com.red.circle.other.app.inner.service.live.RoomUserBlacklistClientService;
import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService;
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.entity.live.RoomMember;
import com.red.circle.other.infra.database.rds.service.live.RoomMemberService;
import com.red.circle.other.inner.asserts.RoomErrorCode;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.enums.config.EnumConfigKey;
import com.red.circle.other.inner.model.cmd.live.RoomBlacklistQryCmd;
import com.red.circle.other.inner.model.dto.live.MicBlockDTO;
import com.red.circle.other.inner.model.dto.live.RoomUserBlacklistDTO;

import java.util.*;

import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.text.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 房间黑名单 服务实现.
 *
 * @author pengliang on 2023/8/11
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RoomUserBlacklistClientServiceImpl implements RoomUserBlacklistClientService {


  private final RoomMemberService roomMemberService;
  private final RoomProfileManagerService roomProfileManagerService;
  private final ImGroupClient imGroupClient;
  private final AgoraClient agoraClient;
  private final UserProfileClient userProfileClient;
  private final EnumConfigCacheService enumConfigCacheService;

  private final RoomUserBlacklistService roomUserBlacklistService;
  private final RoomUserBlacklistInnerConvertor roomUserBlacklistInnerConvertor;

  @Override
  public void removeBlack(Long roomId, Long cmdOriginUserId) {
    roomUserBlacklistService.removeBlack(roomId, cmdOriginUserId);
  }

  @Override
  public void add(RoomUserBlacklistDTO dto) {
    roomUserBlacklistService.add(roomUserBlacklistInnerConvertor.toRoomUserBlacklist(dto));
  }

  @Override
  public List<RoomUserBlacklistDTO> listOps(RoomBlacklistQryCmd qryCmd) {
    return roomUserBlacklistInnerConvertor.toListRoomUserBlacklistDTO(
        roomUserBlacklistService.listOps(qryCmd)
    );
  }

  @Override
  public void bannedLiveUser(MicBlockDTO dto) {
    // 房间不存在
    RoomProfile beBlackRoomProfile = roomProfileManagerService.getProfileById(dto.getRoomId());
    ResponseAssert.notNull(RoomErrorCode.ROOM_NOT_EXISTS, beBlackRoomProfile);
    // 没有找到用户信息,userProfileDTO是将被禁言的用户
    UserProfileDTO userProfileDTO = userProfileClient.getByAccountOne(dto.getAccount()).getBody();
    ResponseAssert.notNull(UserErrorCode.USER_INFO_NOT_FOUND, userProfileDTO);
    // 权限不足: 不可操作管理员
    String superAdminIds = enumConfigCacheService.getValue(EnumConfigKey.ASWAT_ADMINS,
            dto.requireReqSysOrigin());
    ResponseAssert.isFalse(CommonErrorCode.INSUFFICIENT_PERMISSION,
            StringUtils.isNotBlank(superAdminIds) && superAdminIds
                    .contains(String.valueOf(userProfileDTO.getId())));
    // 操作人必须是房主或管理员
    RoomMember requestMember = roomMemberService
            .getRoomMember(dto.getRoomId(), dto.requiredReqUserId());
    ResponseAssert.notNull(UserErrorCode.USER_INFO_NOT_FOUND, requestMember);
    ResponseAssert.isTrue(CommonErrorCode.INSUFFICIENT_PERMISSION,
            requestMember.checkHomeownerOrAdmin());

    // 群成员封禁
    imGroupClient.banGroupMember(new GroupBanCmd()
            .setGroupId(beBlackRoomProfile.getRoomAccount())
            .setUserId(userProfileDTO.getId())
            .setDuration(1440)
            .setDescription(dto.requiredReqUserId()
                    + "#"
                    + requestMember.getRoles()
                    + "#"
                    + userProfileDTO.getId())
    );
    // 请求声网
    agoraClient.kickingRule(dto.getRoomId().toString(), dto.getAccount());
  }

}
