import Message from "./Message.js";
import b4a from "b4a";

class FileMessage extends Message {
    /**
     * @description Creates a new file message.
     * @since 1.0
     * @author MiTask
     * @constructor
     * @param {String} peerName Peer username
     * @param {String} peerAvatar Peer avatar URL
     * @param {String} topic Chat room topic string
     * @param {Number} timestamp UNIX Timestamp
     * @param {String} fileName File name
     * @param {String} fileUrl URL to the file
     * @param {String} fileType Type of the file
     * @param {String} fileData File data in base64 String format
     */
    constructor(peerName, peerAvatar, topic, timestamp, fileName, fileUrl, fileType, fileData) {
        super("file", peerName, peerAvatar, topic, timestamp);
        this.fileName = fileName;
        this.fileUrl = fileUrl;
        this.fileType = fileType;
        this.fileData = fileData; // Add file data property
    }

    /**
     * @since 1.0
     * @author MiTask
     * @returns {String} JSON String with all of the information about the message
     */
    toJsonString() {
        return JSON.stringify({
            ...this.toJson(),
            fileName: this.fileName,
            fileUrl: this.fileUrl,
            fileType: this.fileType,
            file: this.fileData // Include file data in JSON
        });
    }

    /**
     * @description Creates a new file message instance.
     * @since 1.0
     * @author MiTask
     * @param {Client} bot Bot Client class
     * @param {String} fileName File name
     * @param {String} fileUrl URL to the file
     * @param {String} fileType Type of the file
     * @param {Buffer} fileBuffer File data
     * @returns {FileMessage} FileMessage instance.
     */
    static new(bot, fileName, fileUrl, fileType, fileBuffer) {
        const fileData = b4a.toString(fileBuffer, 'base64'); // Convert file buffer to base64
        return new FileMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), fileName, fileUrl, fileType, fileData);
    }
}

export default FileMessage;