/** @param {import(".").NS } ns */



/**
 * get all servers
 * for servers ram > 0 get ram, get money
 * home ram substract ram needed for bots
 * get available threads for each server for hack/grow/weaken
 * sum all money
 * run with proportion to the money available
 */


export async function main(ns) {
    // ns.disableLog('ALL');

    async function writeLog(type, obj) {
        let timeNow = new Date();
        let year = timeNow.getFullYear().toString().substring(2);
        let month = (timeNow.getMonth() < 9 ? '0' : '') + (timeNow.getMonth() + 1);
        let day = (timeNow.getDate() < 10 ? '0' : '') + timeNow.getDate();
        let hour = (timeNow.getHours() < 10 ? '0' : '') + timeNow.getHours();
        let minute = (timeNow.getMinutes() < 10 ? '0' : '') + timeNow.getMinutes();
        let second = (timeNow.getSeconds() < 10 ? '0' : '') + timeNow.getSeconds();
        let timestamp = `${year}${month}${day}_${hour}${minute}${second}`;
        await ns.tryWritePort(2, `t${timestamp}_${type} = ${obj};\n`);

        if (['ERROR', 'TERMINAL'].indexOf(type) != -1) ns.tprint(`${timestamp} ${type} = ${obj}`);
    }

    async function runHack(server) {
        let script = 'simple-hack.js';

        let serverStatus = analyzeServer(server);
        await writeLog('ANALYZE', `{server: "${server}", status: ${JSON.stringify(serverStatus)}}`);

        if (!serverStatus.rootAccess) {
            if (serverStatus.nukeAvailable) {
                await writeLog('NUKE', `"Nuking ${server}!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"`);
                for (let i = 0; i < maxPortsToHack(); ++i) {
                    switch (EXPLOITS[i]) {
                        case 'BruteSSH.exe':
                            ns.brutessh(server);
                            break;

                        case 'FTPCrack.exe':
                            ns.ftpcrack(server);
                            break;

                        case 'relaySMTP.exe':
                            ns.relaysmtp(server);
                            break;

                        case 'HTTPWorm.exe':
                            ns.httpworm(server);
                            break;

                        case 'SQLInject.exe':
                            ns.sqlinject(server);
                            break;

                    }
                }
                ns.nuke(server);
            }
        }

        serverStatus = analyzeServer(server);

        if (serverStatus.rootAccess) {
            if (!ns.fileExists(script, 'home')) {
                await writeLog('ERROR', `"Script '${script}' doesn't exist!"`);
                // return -3;
            }
            ns.scp(script, server, 'home');

            let serverMaxRam = ns.getServerMaxRam(server);
            let serverUsedRam = ns.getServerUsedRam(server);
            let serverFreeRam = serverMaxRam - serverUsedRam;
            let scriptRam = ns.getScriptRam(script, server);
            let maxThreads = Math.floor(serverFreeRam / scriptRam);

            let a = ns.scriptRunning(script, server);
            if (!a) {
                if (maxThreads == 0) {
                    // await writeLog('ERROR', `"Not enough ram to run '${script}' on '${server}', ${scriptRam}/${serverFreeRam}!"`);
                    return -4;
                }

                let execExitCode = ns.exec(script, server, maxThreads, server);

                if (!execExitCode) {
                    // await writeLog('ERROR', `"Exec error running script '${script} on '${server}!"`);
                    return -5;
                }
                else {
                    return execExitCode;
                }
            } else {
                // await writeLog('ERROR', `"Hack already running on ${server}"`);
                return 0;
            }
        }

        return -6;
    }

    function scanServer(currentServer, previousServer = currentServer) {
        let availableServers = ns.scan(currentServer);
        let result = [];

        for (let i = 0; i < availableServers.length; ++i) {
            let nextServer = availableServers[i];

            if (nextServer != currentServer && nextServer != previousServer) {
                result.concat(scanServer(nextServer, currentServer));
            }
        }

        return [currentServer].concat(result);
    }

    while (true) {
        let network = scanServer('home');

        await ns.sleep(100);
    }
}