/**
 * @module Message
 */

import {Message} from "./Message";
import b4a from "b4a";
import {Client} from "../Client";

export class FileMessage extends Message {
    fileName: string;
    fileUrl: string;
    fileType: string;
    fileData: string;
    /**
     * @description Creates a new file 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 fileName File name
     * @param fileUrl URL to the file
     * @param fileType Type of the file
     * @param fileData File data in base64 String format
     */
    constructor(peerName: string, peerAvatar: string, topic: string | null, timestamp: number, fileName: string, fileUrl: string, fileType: string, fileData: string) {
        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(): string {
        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 bot Bot Client class
     * @param fileName File name
     * @param fileUrl URL to the file
     * @param fileType Type of the file
     * @param fileBuffer File data
     * @returns {FileMessage} FileMessage instance.
     */
    static new(bot: Client, fileName: string, fileUrl: string, fileType: string, fileBuffer: Buffer): FileMessage {
        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);
    }
}