/**
 * @module Message
 */

import {Message} from "./Message";
import b4a from "b4a";
import {Client} from "../Client";

export class AudioMessage extends Message {
    audioUrl: string;
    audioType: string;
    audioData: string;
    /**
     * @description Creates a new Audio message.
     * @since 1.0
     * @author MiTask
     * @constructor
     * @param peerName Peer username
     * @param peerAvatar Peer avatar URL
     * @param topic Chat room topic string
     * @param timestamp UNIX Timestamp
     * @param audioUrl URL to the audio file
     * @param audioType Type of the audio file
     * @param audioData Audio file data in base64 String format
     */
    constructor(peerName: string, peerAvatar: string, topic: string | null, timestamp: number, audioUrl: string, audioType: string, audioData: string) {
        super("audio", peerName, peerAvatar, topic, timestamp);
        this.audioUrl = audioUrl;
        this.audioType = audioType;
        this.audioData = audioData; // Add audio data property
    }

    /**
     * @since 1.0
     * @author MiTask
     * @returns {String} JSON String with all of the information about the message
     */
    toJsonString(): string {
        return JSON.stringify({
            ...this.toJson(),
            audioUrl: this.audioUrl,
            audioType: this.audioType,
            audio: this.audioData // Include audio data in JSON
        });
    }

    /**
     * @description Creates a new audio message instance.
     * @since 1.0
     * @author MiTask
     * @param bot Bot Client class
     * @param audioUrl URL to the audio file
     * @param audioType Type of the audio file
     * @param audioBuffer Audio file data
     * @returns {AudioMessage} AudioMessage instance.
     */
    static new(bot: Client, audioUrl: string, audioType: string, audioBuffer: Buffer): AudioMessage {
        const audioData = b4a.toString(audioBuffer, 'base64'); // Convert audio buffer to base64
        return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audioUrl, audioType, audioData);
    }
}
