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

import com.red.circle.component.instant.msg.tencet.service.endpoint.api.im.account.ImAccountManagerService;
import com.red.circle.component.instant.msg.tencet.service.endpoint.api.im.profile.ImProfileManagerService;
import com.red.circle.component.instant.msg.tencet.service.param.BaseProfile;
import com.red.circle.component.instant.msg.tencet.service.param.IMGenderEnum;
import com.red.circle.component.instant.msg.tencet.service.param.ImAccountImportParam;
import com.red.circle.external.infra.common.OfficialNoticeCommonService;
import com.red.circle.external.inner.model.cmd.message.RegisterAccountCmd;
import com.red.circle.external.inner.model.enums.message.OfficialNoticeTypeEnum;
import com.red.circle.external.inner.service.message.ImAccountClientService;
import com.red.circle.tool.core.collection.MapBuilder;
import com.red.circle.tool.core.thread.ThreadPoolManager;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * im账号管理 实现.
 *
 * @author pengliang on 2023/7/12
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ImAccountClientServiceImpl implements ImAccountClientService {

  private final ImAccountManagerService imAccountManagerService;
  private final ImProfileManagerService imProfileManagerService;
  private final OfficialNoticeCommonService officialNoticeCommonService;

  @Override
  public String createUserSig(Long userId) {
    return imAccountManagerService.genUserSig(userId);
  }

  @Override
  public void kick(Long userId) {
    ThreadPoolManager.getInstance()
        .execute(() -> imAccountManagerService.kick(userId));
  }

  @Override
  public void register(RegisterAccountCmd cmd) {
    imAccountManagerService.accountImportAsync(
            ImAccountImportParam.builder()
                    .faceUrl(cmd.getAvatar())
                    .identifier(Objects.toString(cmd.getIdentifier()))
                    .nick(cmd.getNickname())
                    .build(), res -> {
              // 更新IM资料设置
              imProfileManagerService.portraitSet(BaseProfile.builder()
                      .account(Objects.toString(cmd.getIdentifier()))
                      .nickname(cmd.getNickname())
                      .gender(IMGenderEnum.getEnumByValue(cmd.getGender()))
                      .build());
              // 发送问候语
              officialNoticeCommonService
                      .send(cmd.getIdentifier(),
                              cmd.getLang(),
                              OfficialNoticeTypeEnum.REGISTER_WELCOME_MESSAGE,
                              MapBuilder.builder().put("nickname", cmd.getNickname()).build()
                      );
            }, er -> log.error("im:{}", er)
    );
  }

  @Override
  public void portraitSetAvatar(Long userId, String avatar) {
    imProfileManagerService.portraitSet(BaseProfile.builder()
        .account(Objects.toString(userId))
        .avatar(avatar)
        .build());
  }

  @Override
  public void portraitSetNickname(Long userId, String nickname) {
    imProfileManagerService.portraitSet(BaseProfile.builder()
        .account(Objects.toString(userId))
        .nickname(nickname)
        .build());
  }

}
