/* lib/terminal/commands/library/agent_online - A connectivity tester to shared remote agents. */ import common from "../../../common/common.js";
import error from "../../utilities/error.js";
import ipResolve from "../../server/transmission/ipResolve.js";
import readStorage from "../../utilities/readStorage.js";
import vars from "../../utilities/vars.js";
const agentOnline = function terminal_commands_library_agentOnline(callback) {
    vars.settings.verbose = true;
    readStorage(true, function terminal_commands_library_agentOnline_readStorage(settings) {
        const arg = process.argv[0], type = settings.agents.device[arg] === undefined ? "user" : "device", hash = settings.identity.hashDevice;
        let title = "";
        if (Object.keys(settings.agents.device).length < 1) {
            error([
                `${vars.text.angry}Device data is not present in settings.${vars.text.angry}`,
                `Run the ${vars.text.cyan}service${vars.text.none} command and go to address ${vars.text.cyan + vars.network.domain[0] + vars.text.none} in the web browser to initiate device data.`
            ], null, true);
            return;
        }
        if (arg === "list") {
            const store = [];
            common.agents({
                countBy: "agent",
                perAgent: function terminal_commands_library_agentOnline_readStorage_perAgent(agentNames) {
                    const text = `${vars.text.angry}*${vars.text.none} ${vars.text.green + agentNames.agent + vars.text.none} - ${settings.agents[agentNames.agentType][agentNames.agent].name}, ${settings.agents[agentNames.agentType][agentNames.agent].ipSelected}`;
                    if (agentNames.agent === hash) {
                        store.push(text.replace(" - ", ` - ${vars.text.angry}(local device)${vars.text.none} - `));
                    } else {
                        store.push(text);
                    }
                },
                perAgentType: function terminal_commands_library_agentOnline_readStorage_perAgentType(agentNames) {
                    store.push("");
                    store.push(`${vars.text.cyan + vars.text.bold + common.capitalize(agentNames.agentType)}:${vars.text.none}`);
                    if (agentNames.agentType === "user" && Object.keys(settings.agents.user).length < 1) {
                        store.push("no shared users");
                    }
                },
                source: settings
            });
            callback("Agent List", store, null);
        } else {
            const report = function terminal_commands_library_agentOnline_readStorage_report(summary) {
                const output = [], data = (arg === "all" || arg === "device" || arg === "user") && summary !== "none" ? JSON.parse(summary) : null, devices = data !== null ? Object.keys(data.device) : [], users = data !== null ? Object.keys(data.user) : [], star = `${vars.text.angry}*${vars.text.none}`, offline = function terminal_commands_library_agentOnline_readStorage_report_offline(hash) {
                    return vars.text.angry + hash + vars.text.none;
                }, online = function terminal_commands_library_agentOnline_readStorage_report_online(hash) {
                    return vars.text.green + hash + vars.text.none;
                };
                let a = devices.length;
                if (arg === "all" || arg === "device") {
                    output.push(`${vars.text.cyan + vars.text.bold}Devices:${vars.text.none}`);
                    output.push(`${star} ${online(vars.identity.hashDevice)} - ${offline("(local device)")} - ${vars.agents.device[vars.identity.hashDevice].name}`);
                    if (a > 0) {
                        do {
                            a = a - 1;
                            if (data.device[devices[a]] === "unknown") {
                                output.push(`${star} ${offline(devices[a])} - ${vars.text.angry}unknown device${vars.text.none}`);
                            } else if (data.device[devices[a]] === "online") {
                                output.push(`${star} ${online(devices[a])} - ${vars.agents.device[devices[a]].name}, ${vars.agents.device[devices[a]].ipSelected}`);
                            } else if (data.device[devices[a]] !== "self") {
                                output.push(`${star} ${offline(devices[a])} - ${vars.agents.device[devices[a]].name}, ${vars.text.angry + data.device[devices[a]] + vars.text.none}`);
                            }
                        }while (a > 0)
                    }
                }
                if (arg === "all" || arg === "user") {
                    if (arg === "all") {
                        output.push("");
                    }
                    output.push(`${vars.text.cyan + vars.text.bold}Users:${vars.text.none}`);
                    a = users.length;
                    if (a > 0) {
                        do {
                            a = a - 1;
                            if (data.device[devices[a]] === "unknown") {
                                output.push(`${star} ${offline(users[a])} - ${vars.text.angry}unknown user${vars.text.none}`);
                            } else if (data.device[devices[a]] === "online") {
                                output.push(`${star} ${online(users[a])} - ${vars.agents.device[users[a]].name}, ${vars.agents.user[users[a]].ipSelected}`);
                            } else {
                                output.push(`${star} ${offline(users[a])} - ${vars.agents.device[users[a]].name}, ${vars.text.angry + data.user[users[a]] + vars.text.none}`);
                            }
                        }while (a > 0)
                    } else {
                        output.push("No shared users");
                    }
                }
                if (arg !== "all" && arg !== "device" && arg !== "user") {
                    if (summary === "unknown") {
                        output.push(`${star} ${offline(arg)} - ${vars.text.angry}unknown ${type + vars.text.none}`);
                    } else if (summary === "online") {
                        output.push(`${star} ${online(arg)} - ${vars.agents[type][arg].name}, ${vars.agents[type][arg].ipSelected}`);
                    } else {
                        output.push(`${star} ${offline(arg)} - ${vars.agents[type][arg].name}, ${vars.text.angry + summary + vars.text.none}`);
                    }
                }
                vars.settings.verbose = true;
                callback(title, output, null);
            };
            if (arg === "all") {
                title = "Test All Agent Connectivity";
            } else if (arg === "device" || arg === "user") {
                title = `Test Each ${common.capitalize(arg)} Agent`;
            } else {
                title = "Agent test for Single Agent";
            }
            if (arg !== "all" && arg !== "device" && arg !== "user" && vars.agents[type][arg] === undefined) {
                error([
                    `${vars.text.angry}Parameter ${arg} is either not an accepted agent identifier or is not present in settings files device.json or user.json.${vars.text.none}`
                ], null, true);
                return;
            }
            if (arg === hash) {
                callback(title, [
                    `The requested agent is this local device.  ${vars.text.angry}No connectivity test performed.${vars.text.none}`
                ], null);
                return;
            }
            ipResolve(arg, type, report);
        }
    });
};
export default agentOnline;
