import { AccessToken, priviledges } from './AccessToken';

export enum Role {
  // DEPRECATED. Role::ATTENDEE has the same privileges as Role.PUBLISHER.
  ATTENDEE = 0,

  // RECOMMENDED. Use this role for a voice/video call or a live broadcast
  PUBLISHER = 1,

  // Only use this role if your scenario require authentication for Co-host
  SUBSCRIBER = 2,

  // DEPRECATED. Role.ADMIN has the same privileges as Role.PUBLISHER.
  ADMIN = 101,
}

export class RtcTokenBuilder {
  /**
   * Builds an RTC token using an Integer uid.
   * @param appID - The App ID issued to you by Agora.
   * @param appCertificate - Certificate of the application that you registered in the Agora Dashboard.
   * @param channelName - The unique channel name for the AgoraRTC session in the string format.
   * @param uid - User ID. A 32-bit unsigned integer with a value ranging from 1 to (2^32-1).
   * @param role - See #userRole.
   * @param privilegeExpiredTs - represented by the number of seconds elapsed since 1/1/1970.
   * @returns The new Token.
   */
  public static buildTokenWithUid(appID: string, appCertificate: string, channelName: string, uid: number, role: Role, privilegeExpiredTs: number): string {
    return this.buildTokenWithAccount(appID, appCertificate, channelName, uid, role, privilegeExpiredTs);
  }

  /**
   * Builds an RTC token with account.
   * @param appID - The App ID issued to you by Agora.
   * @param appCertificate - Certificate of the application that you registered in the Agora Dashboard.
   * @param channelName - The unique channel name for the AgoraRTC session in the string format.
   * @param account - The user account.
   * @param role - See #userRole.
   * @param privilegeExpiredTs - represented by the number of seconds elapsed since 1/1/1970.
   * @returns The new Token.
   */
  public static buildTokenWithAccount(
    appID: string,
    appCertificate: string,
    channelName: string,
    account: number | string,
    role: Role,
    privilegeExpiredTs: number
  ): string {
    const key = new AccessToken(appID, appCertificate, channelName, account);
    key.addPriviledge(priviledges.kJoinChannel, privilegeExpiredTs);
    if (role === Role.ATTENDEE || role === Role.PUBLISHER || role === Role.ADMIN) {
      key.addPriviledge(priviledges.kPublishAudioStream, privilegeExpiredTs);
      key.addPriviledge(priviledges.kPublishVideoStream, privilegeExpiredTs);
      key.addPriviledge(priviledges.kPublishDataStream, privilegeExpiredTs);
    }
    return key.build();
  }
}
