/*
 * RCCallUni - v5.30.0
 * CommitId - cf4b2f0ffe2ae11c39bbfc216890745f254753c9
 * Tue Dec 02 2025 16:50:41 GMT+0800 (China Standard Time)
 * ©2020 RongCloud, Inc. All rights reserved.
 */

class Logger {
  tag;

  constructor(tag) {
    this.tag = tag;
  }

  log(str) {
    console.log(`[${this.tag}] ${new Date().toISOString()} ${str}`);
  }
}

const logger = new Logger('RCUNICallLib');

// 枚举定义
export const RCCallIWCamera = {
  NONE: 0,
  FRONT: 1,
  BACK: 2
};

export const RCCallIWMediaType = {
  AUDIO: 0,
  AUDIO_VIDEO: 1
};

export const RCUNICallErrorCode = {
  SUCCESS: 0,
  ENGINE_NOT_FOUND: 1,
  NETWORK_UNAVAILABLE: 2,
  ONE_CALL_EXISTED: 3,
  OPERATION_UNAVAILABLE: 4,
  INVALID_PARAM: 5,
  NETWORK_UNSTABLE: 6,
  MEDIA_REQUEST_FAILED: 7,
  MEDIA_SERVER_NOT_READY: 8,
  MEDIA_SERVER_NOT_INITIALIZED: 9,
  MEDIA_REQUEST_TIMEOUT: 10,
  MEDIA_UNKOWN_ERROR: 11,
  MEDIA_KICKED_BY_SERVER_ERROR: 12,
  MEDIA_SERVER_CLOSED_ERROR: 13,
  MEDIA_SERVER_PUBLISH_ERROR: 14,
  MEDIA_SERVER_SUBSCRIBE_ERROR: 15,
  MEDIA_JOIN_ROOM_REFUSE_ERROR: 16
};

export const RCCallIWCallDisconnectedReason = {
  CANCEL: 0,
  REJECT: 1,
  HANGUP: 2,
  BUSY_LINE: 3,
  NO_RESPONSE: 4,
  ENGINE_UNSUPPORTED: 5,
  NETWORK_ERROR: 6,
  RESOURCE_GET_ERROR: 7,
  RESOURCE_PUBLISH_ERROR: 8,
  RESOURCE_SUBSCRIBE_ERROR: 9,
  REMOTE_CANCEL: 10,
  REMOTE_REJECT: 11,
  REMOTE_HANGUP: 12,
  REMOTE_BUSY_LINE: 13,
  REMOTE_NO_RESPONSE: 14,
  REMOTE_ENGINE_UNSUPPORTED: 15,
  REMOTE_NETWORK_ERROR: 16,
  REMOTE_RESOURCE_GET_ERROR: 17,
  REMOTE_RESOURCE_PUBLISH_ERROR: 18,
  REMOTE_RESOURCE_SUBSCRIBE_ERROR: 19,
  KICKED_BY_OTHER_CALL: 20,
  IN_OTHER_CALL: 21,
  KICKED_BY_SERVER: 22,
  REMOTE_KICKED_BY_OTHER_CALL: 23,
  REMOTE_IN_OTHER_CALL: 24,
  REMOTE_KICKED_BY_SERVER: 25,
  ACCEPT_BY_OTHER_CLIENT: 26,
  HANGUP_BY_OTHER_CLIENT: 27,
  REJECTED_BY_BLACKLIST: 28,
  SERVICE_NOT_OPENED: 29,
  DROP_TO_OBSERVER: 30,
  INIT_VIDEO_ERROR: 31,
  OTHER_DEVICE_HAD_ACCEPTED: 32,
  SERVICE_DISCONNECTED: 33
};

export const RCCallIWAudioCodecType = {
  PCMU: 0,
  OPUS: 111
};

export const RCCallIWAudioSource = {
  DEFAULT: 0,
  MIC: 1,
  VOICE_UPLINK: 2,
  VOICE_DOWNLINK: 3,
  VOICE_CALL: 4,
  CAMCORDER: 5,
  VOICE_RECOGNITION: 6,
  VOICE_COMMUNICATION: 7
};

export const RCCallIWVideoProfile = {
  PROFILE_144_256: 0,
  PROFILE_240_240: 1,
  PROFILE_240_320: 2,
  PROFILE_360_480: 3,
  PROFILE_360_640: 4,
  PROFILE_480_640: 5,
  PROFILE_480_720: 6,
  PROFILE_720_1280: 7,
  PROFILE_1080_1920: 8,
  PROFILE_144_256_HIGH: 9,
  PROFILE_240_240_HIGH: 10,
  PROFILE_240_320_HIGH: 11,
  PROFILE_360_480_HIGH: 12,
  PROFILE_360_640_HIGH: 13,
  PROFILE_480_640_HIGH: 14,
  PROFILE_480_720_HIGH: 15,
  PROFILE_720_1280_HIGH: 16,
  PROFILE_1080_1920_HIGH: 17
};

export const RCCallIWCameraOrientation = {
  PORTRAIT: 0,
  PORTRAIT_UPSIDE_DOWN: 1,
  LANDSCAPE_RIGHT: 2,
  LANDSCAPE_LEFT: 3
};

export const RCCallIWVideoBitrateMode = {
  CQ: 0,
  VBR: 1,
  CBR: 2
};

export const RCCallIWNetworkQuality = {
  UNKNOWN: 0,
  EXCELLENT: 1,
  GOOD: 2,
  POOR: 3,
  BAD: 4,
  TERRIBLE: 5
};

export const RCCallIWImportanceHW = {
  NORMAL: 0,
  LOW: 1
};

export const RCCallIWCallType = {
  SINGLE: 0,
  GROUP: 1
};

export const RCCallIWUserType = {
  NORMAL: 0,
  OBSERVER: 1
};

const call = uni.requireNativePlugin('RongCloud-Call-RCUniCall');
const Platform = uni.getSystemInfoSync().platform;

export function init() {
  logger.log(`init Platform->${Platform}`);
  call.init({});
}

export function unInit() {
  logger.log(`unInit Platform->${Platform}`);
  call.unInit();
}

export function onCallReceived(listener) {
  logger.log(`Engine:OnCallReceived 监听通话呼入 Platform->${Platform}`);
  call.addEventListener("Engine:OnCallReceived", listener);
}

export function removeCallReceivedListener() {
  logger.log(`removeCallReceivedListener`);
  call.removeAllEventListeners('Engine:OnCallReceived');
}

export function onCallConnected(listener) {
  logger.log(`Engine:OnCallConnected 已建立通话通话接通 Platform->${Platform}`);
  call.addEventListener("Engine:OnCallConnected", listener);
}

export function removeCallConnectedListener() {
  logger.log(`removeCallConnectedListener`);
  call.removeAllEventListeners('Engine:OnCallConnected');
}

export function onCallDisconnected(listener) {
  logger.log(`Engine:OnCallDisconnected 监听通话结束 Platform->${Platform}`);
  call.addEventListener("Engine:OnCallDisconnected", listener);
}

export function removeCallDisconnectedListener() {
  logger.log(`removeCallDisconnectedListener`);
  call.removeAllEventListeners('Engine:OnCallDisconnected');
}

export function onRemoteUserInvited(listener) {
  logger.log(`Engine:OnRemoteUserInvited 通话中的某一个参与者，邀请好友加入通话  Platform->${Platform}`);
  call.addEventListener("Engine:OnRemoteUserInvited", listener);
}

export function removeRemoteUserInvited() {
  logger.log(`removeRemoteUserInvited`);
  call.removeAllEventListeners('Engine:OnRemoteUserInvited');
}

export function onRemoteUserJoined(listener) {
  logger.log(`Engine:OnRemoteUserJoined 对端用户加入通话 Platform->${Platform}`);
  call.addEventListener("Engine:OnRemoteUserJoined", listener);
}

export function removeRemoteUserJoinedListener() {
  logger.log(`removeRemoteUserJoinedListener`);
  call.removeAllEventListeners('Engine:OnRemoteUserJoined');
}

export function onRemoteUserLeft(listener) {
  logger.log(`Engine:OnRemoteUserLeft 对端用户挂断(群聊触发) Platform->${Platform}`);
  call.addEventListener("Engine:OnRemoteUserLeft", listener);
}

export function removeRemoteUserLeftListener() {
  logger.log(`removeRemoteUserLeftListener`);
  call.removeAllEventListeners('Engine:OnRemoteUserLeft');
}

export function onCallOutgoing(listener) {
  logger.log(`Engine:OnCallOutgoing 电话已拨出，通知当前 call 的详细信息 Platform->${Platform}`);
  call.addEventListener("Engine:OnCallOutgoing", listener);
}

export function removeCallOutgoingListener() {
  logger.log(`removeCallOutgoingListener`);
  call.removeAllEventListeners('Engine:OnCallOutgoing');
}

export function onRemoteUserRinging(listener) {
  logger.log(`Engine:OnRemoteUserRinging 被叫端收到请求，发出振铃响应时监听 Platform->${Platform}`);
  call.addEventListener("Engine:OnRemoteUserRinging", listener);
}

export function removeRemoteUserRingingListener() {
  logger.log(`removeRemoteUserRingingListener`);
  call.removeAllEventListeners('Engine:OnRemoteUserRinging');
}

export function onError(listener) {
  logger.log(`Engine:OnError 通话出现错误 Platform->${Platform}`);
  call.addEventListener("Engine:OnError", listener);
}

export function removeErrorListener() {
  logger.log(`removeErrorListener`);
  call.removeAllEventListeners('Engine:OnError');
}

export function onRemoteUserMediaTypeChanged(listener) {
  logger.log(`Engine:OnRemoteUserMediaTypeChanged 对端用户切换了媒体类型 Platform->${Platform}`);
  call.addEventListener("Engine:OnRemoteUserMediaTypeChanged", listener);
}

export function removeRemoteUserMediaTypeChangedListener() {
  logger.log(`removeRemoteUserMediaTypeChangedListener`);
  call.removeAllEventListeners('Engine:OnRemoteUserMediaTypeChanged');
}

export function onRemoteUserCameraStateChanged(listener) {
  logger.log(`Engine:OnRemoteUserCameraStateChanged 对端用户关闭了摄像头 Platform->${Platform}`);
  call.addEventListener("Engine:OnRemoteUserCameraStateChanged", listener);
}

export function removeRemoteUserCameraStateChangedListener() {
  logger.log(`removeRemoteUserCameraStateChangedListener`);
  call.removeAllEventListeners('Engine:OnRemoteUserCameraStateChanged');
}

export function onRemoteUserMicrophoneStateChanged(listener) {
  logger.log(`Engine:OnRemoteUserMicrophoneStateChanged 对端用户开关麦克风 Platform->${Platform}`);
  call.addEventListener("Engine:OnRemoteUserMicrophoneStateChanged", listener);
}

export function removeRemoteUserMicrophoneStateChangedListener() {
  logger.log(`removeRemoteUserMicrophoneStateChanged`);
  call.removeAllEventListeners('Engine:OnRemoteUserMicrophoneStateChanged');
}

export function onNetworkQuality(listener) {
  logger.log(`Engine:OnNetworkQuality Platform->${Platform}`);
  call.addEventListener("Engine:OnNetworkQuality", listener);
}

export function removeNetworkQualityListener() {
  call.removeAllEventListeners('Engine:OnNetworkQuality');
}

export function onEnableCamera(listener) {
  logger.log(`Engine:OnEnableCamera Platform->${Platform}`);
  call.addEventListener("Engine:OnEnableCamera", listener);
}

export function removeEnableCameraListener() {
  logger.log(`removeEnableCameraListener`);
  call.removeAllEventListeners('Engine:OnEnableCamera');
}

export function onSwitchCamera(listener) {
  logger.log(`Engine:OnSwitchCamera Platform->${Platform}`);
  call.addEventListener("Engine:OnSwitchCamera", listener);
}

export function removeSwitchCameraListener() {
  logger.log(`removeSwitchCameraListener`);
  call.removeAllEventListeners('Engine:OnSwitchCamera');
}

export function getCurrentCallSession() {
  logger.log(`getCurrentCallSession Platform->${Platform}`);
  return call.getCurrentCallSession();
}

export function hangup() {
  logger.log(`hangup Platform->${Platform}`);
  call.hangup();
}

export function accept() {
  logger.log(`accept Platform->${Platform}`);
  call.accept();
}

export function startSingleCall(targetId, type, extra) {
  logger.log(`startSingleCall targetId->${targetId} type=>${type} extra=>${extra} Platform->${Platform}`);
  call.startSingleCall(targetId, type, extra);
}

export function startGroupCall(groupId, userIds, observerUserIds, type, extra) {
  logger.log(`startGroupCall groupId->${groupId} userIds=>${userIds} observerUserIds=>${observerUserIds} type=>${type} extra=>${extra} Platform->${Platform}`);
  call.startGroupCall(groupId, userIds, observerUserIds, type, extra);
}

export function inviteUsers(userIds, observerUserIds) {
  logger.log(`inviteUsers 邀请用户->${userIds} Platform->${Platform}`);
  call.inviteUsers(userIds, observerUserIds);
}

export function setVideoView(userId, ref, type, isZOrderOnTop = false) {
  logger.log(`setVideoView userId->${userId} ref=>${ref} type=>${type} isZOrderOnTop=>${isZOrderOnTop} Platform->${Platform}`);
  Platform === 'android' ? call.setVideoView(userId, ref, type, isZOrderOnTop) : call.setVideoView(userId, ref, type);
}

export function enableSpeaker(isOpen) {
  logger.log(`enableSpeaker isOPen->${isOpen} Platform->${Platform}`);
  call.enableSpeaker(isOpen);
}

export function enableMicrophone(isOpen) {
  logger.log(`enableMicrophone isOPen->${isOpen} Platform->${Platform}`);
  call.enableMicrophone(isOpen);
}

export function switchCamera() {
  logger.log(`switchCamera Platform->${Platform}`);
  call.switchCamera();
}

export function currentCamera() {
  logger.log(`currentCamera Platform->${Platform}`);
  return call.currentCamera();
}

export function enableCamera(isOpen, camera) {
  logger.log(`enableCamera isOpen=>${isOpen} camera=>${camera} Platform->${Platform}`);
  call.enableCamera(isOpen, camera);
}

export function changeMediaType(type) {
  logger.log(`changeMediaType type=>${type}  Platform->${Platform}`);
  call.changeMediaType(type);
}

export function setAudioConfig(config) {
  logger.log(`setAudioConfig config=>${config}  Platform->${Platform}`);
  call.setAudioConfig(config);
}

export function setVideoConfig(config) {
  logger.log(`setVideoConfig config=>${config}  Platform->${Platform}`);
  call.setVideoConfig(config);
}

export function setPushConfig(config, hangupPushConfig) {
  logger.log(`setPushConfig config=>${config} hangupPushConfig=>${hangupPushConfig} Platform->${Platform}`);
  call.setPushConfig(config, hangupPushConfig);
}