package com.red.circle.external.inner.service.message.impl;

import com.red.circle.component.instant.msg.tencet.service.endpoint.api.im.group.ImGroupMemberService;
import com.red.circle.component.instant.msg.tencet.service.enums.ImGroupSilenceType;
import com.red.circle.external.inner.model.cmd.message.GroupMemberAddCmd;
import com.red.circle.external.inner.model.cmd.message.GroupMemberRemoveCmd;
import com.red.circle.external.inner.service.message.ImGroupMemberClientService;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.json.JacksonUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 群成员.
 *
 * @author pengliang on 2023/12/28
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ImGroupMemberClientServiceImpl implements ImGroupMemberClientService {

  private final ImGroupMemberService imGroupMemberService;

  @Override
  public ResultResponse<Void> addGroupMember(GroupMemberAddCmd cmd) {
    if (CollectionUtils.isEmpty(cmd.getAccounts())) {
      return ResultResponse.success();
    }

    imGroupMemberService.addGroupMember(cmd.getGroupId(), cmd.getAccounts(),
        Objects.equals(cmd.getSilenceType(), Boolean.TRUE)
            ? ImGroupSilenceType.Yes : ImGroupSilenceType.No).subscribe();
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<Void> removeGroupMember(GroupMemberRemoveCmd cmd) {

    if (CollectionUtils.isEmpty(cmd.getAccounts())) {
      return ResultResponse.success();
    }

    imGroupMemberService.removeGroupMember(cmd.getGroupId(), cmd.getAccounts(),
        Objects.equals(cmd.getSilenceType(), Boolean.TRUE)
            ? ImGroupSilenceType.Yes : ImGroupSilenceType.No).subscribe();
    return ResultResponse.success();
  }

}
