import { world } from "@minecraft/server";

export const testing = {
    _lastUsedTime: new Date(0).getTime(), // Variable to store the last used time for any method
    _id: 0,
    _isTesting: false,

    /**
     * Prints a message in the chat with a warning (red) color.
     * @param {...string} messageParts - Parts of the message to join and print
     */
    warn: function(...messageParts) {
        if (!this._isTesting) return;
        const currentTime = new Date();
        this._checkAndLogSeparator(currentTime);
        const coloredMessage = "§c" + messageParts.join(" ") + "§r"; // §c for red and §r to reset
        world.sendMessage(++this._id + ". " + coloredMessage);
    },
    
    /**
     * Prints a message in the chat with an info (blue) color.
     * @param {...string} messageParts - Parts of the message to join and print
     */
    info: function(...messageParts) {
        if (!this._isTesting) return;
        const currentTime = Date.now();
        this._checkAndLogSeparator(currentTime);
        const coloredMessage = "§9" + messageParts.join(" ") + "§r"; // §9 for blue and §r to reset
        world.sendMessage(++this._id + ". " + coloredMessage);
    },
    
    /**
     * Prints a message in the chat with a success (green) color.
     * @param {...string} messageParts - Parts of the message to join and print
     */
    success: function(...messageParts) {
        if (!this._isTesting) return;
        const currentTime = Date.now();
        this._checkAndLogSeparator(currentTime);
        const coloredMessage = "§a" + messageParts.join(" ") + "§r"; // §a for green and §r to reset
        world.sendMessage(++this._id + ". " + coloredMessage);
    },
    
    /**
     * Prints a message in the chat with an error (dark red) color.
     * @param {...string} messageParts - Parts of the message to join and print
     */
    error: function(...messageParts) {
        if (!this._isTesting) return;
        const currentTime = Date.now();
        this._checkAndLogSeparator(currentTime);
        const coloredMessage = "§4" + messageParts.join(" ") + "§r"; // §4 for dark red and §r to reset
        world.sendMessage(++this._id + ". " + coloredMessage);
    },

    /**
     * Checks the last used time of any method and logs a separator if necessary.
     * @param {number} currentTime - Current timestamp
     */
    _checkAndLogSeparator: function(currentTime) {
        const lastUsedTime = this._lastUsedTime;
        const timePassed = lastUsedTime ? (currentTime - lastUsedTime) : null;
        if (timePassed > 200) {
            world.sendMessage("===============");
            this._id = 0;
        }
        this._lastUsedTime = currentTime;
    }
}

export function clickTest(data) {
    const dateStamp = Date.now();
    const stamp = data[Symbol.for('dateStamp')];
    data[Symbol.for('dateStamp')] = dateStamp;
    return dateStamp - (stamp ?? 0)
};

export function multiClicking(data, sensitivity = 100) {
    return clickTest(data) < sensitivity
}

/**
 * Returns true if chance succeeds
 * 
 * @param {number} chance - A number between 0 and 1 representing the probability.
 * @returns {boolean} - True if the random number is less than the chance, otherwise false.
 */
export function randomChance(chance) {
    return Math.random() < chance;
}

export function randomInteger(max, min = 0) {
    return Math.floor(Math.random() * (max - min + 1)) + min
}

export function randomFloat(max, min = 0, places = 2) {
    const factor = Math.pow(10, places);
    return (Math.random() * (max - min) + min).toFixed(places) * 1;
}

/**
 * 
 * @param {any[]} array 
 * @param {any} data 
 */
export function arrayIncludes(array, data) {
    return array.some(element => element == data)
}

