/** @param {import(".").NS } ns */
export async function main(ns) {
    // ns.disableLog('ALL');
    const doc = eval('document');

    function numberToUnits(number) {
        let numberString = Math.floor(number);
        if (Math.abs(number) / 1000 >= 1) {
            numberString = Math.floor(number / 1000) + 'k';
        }
        if (Math.abs(number) / 1000000 >= 1) {
            numberString = Math.floor(number / 1000000) + 'm';
        }
        if (Math.abs(number) / 1000000000 >= 1) {
            numberString = Math.floor(number / 1000000000) + 'b';
        }
        if (Math.abs(number) / 1000000000000 >= 1) {
            numberString = Math.floor(number / 1000000000000) + 't';
        }

        return numberString;
    }

    let lastMoney = ns.getServerMoneyAvailable('home');
    let lastTime = Date.now();
    const firstMoney = ns.getServerMoneyAvailable('home');
    const firstTime = Date.now();

    while (true) {
        const currentMoney = ns.getServerMoneyAvailable('home');
        const moneyDiff = currentMoney - lastMoney;
        const currentTime = Date.now();
        const timeDiff = (currentTime - lastTime) / 1000;
        const moneyPerSecond = moneyDiff / timeDiff;
        const moneyDiffLife = currentMoney - firstMoney;
        const timeDiffLife = (currentTime - firstTime) / 1000;
        const moneyPerSecondLife = moneyDiffLife / timeDiffLife;
        const moneyString = `${numberToUnits(Math.floor(moneyPerSecond))} | ${numberToUnits(Math.floor(moneyPerSecondLife))} $/s`;
        ns.print(`${lastMoney} ${currentMoney} ${moneyDiff} ${numberToUnits(Math.floor(moneyPerSecondLife)) }\n ${currentTime} ${currentTime} ${timeDiff}`);
        doc.querySelector("#root > div.react-draggable > div.drag.MuiBox-root > div > p").innerText = moneyString;
        ns.write('moneyStats.txt', moneyString+'\n', 'a');
        lastMoney = currentMoney;
        lastTime = currentTime;
        await ns.sleep(5000);
    }
}