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

import com.red.circle.component.instant.msg.agora.AgoraAuthentication;
import com.red.circle.component.instant.msg.agora.AgoraProperties;
import com.red.circle.component.instant.msg.tencet.service.endpoint.api.trtc.TencentTrtcService;
import com.red.circle.external.inner.model.cmd.message.TrtcRemoveUserCmd;
import com.red.circle.external.inner.model.cmd.message.TrtcUserBlockedCmd;
import com.red.circle.external.inner.service.message.TrtcClientService;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import io.agora.media.RtcTokenBuilder2;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * trtc 服务.
 *
 * @author pengliang on 2023/10/17
 */
@Service
@RequiredArgsConstructor
public class TrtcClientServiceImpl implements TrtcClientService {

  private final UserProfileClient userProfileClient;
  private final TencentTrtcService tencentTrtcService;
//  private final AgoraAuthentication agoraAuthentication;
//  private final AgoraProperties agoraProperties;

  @Value("${rtc.appId}")
  private  String appId;

  @Value("${rtc.certificate}")
  private String certificate;

  @Override
  public boolean removeUser(TrtcRemoveUserCmd cmd) {
    return tencentTrtcService.removeUser(cmd.getRoomId(),
        cmd.getUserIds(), cmd.getLogOrigin());
  }

  @Override
  public boolean setUserBlocked(TrtcUserBlockedCmd cmd) {
    return tencentTrtcService.setUserBlocked(cmd.getRoomId(), cmd.getUserId(), cmd.getMute());
  }

  @Override
  public boolean dismissRoom(String roomId) {
    return tencentTrtcService.dismissRoom(roomId);
  }

  @Override
  public String createAgoraToken(Long userId, String channelName) {
    UserProfileDTO userProfile = userProfileClient.getByUserId(userId).getBody();
//    return agoraAuthentication.createChannelToken(
//        channelName,
//        Integer.parseInt(userProfile.getAccount())
//    );
    RtcTokenBuilder2 var3 = new RtcTokenBuilder2();
    return var3.buildTokenWithUid(appId, certificate, channelName, Integer.parseInt(userProfile.getAccount()), RtcTokenBuilder2.Role.ROLE_SUBSCRIBER, 21600, 21600);
  }

  @Override
  public String createAgoraTokenGoUp(Long userId, String channelName) {
    UserProfileDTO userProfile = userProfileClient.getByUserId(userId).getBody();
    RtcTokenBuilder2 var3 = new RtcTokenBuilder2();
    return var3.buildTokenWithUid(appId, certificate, channelName, Integer.parseInt(userProfile.getAccount()), RtcTokenBuilder2.Role.ROLE_PUBLISHER, 21600, 21600);
  }

}
