// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![
            receive_messages,
            send_message
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

#[tauri::command]
fn receive_messages(hostname: &str, password: &str) -> Vec<(u128, String, String, String)> {
    let msges = client_shared::utils::rx_messages(hostname, password, 2);

    let Ok(msges) = msges else {
        return vec![];
    };

    let mut out = vec![];
    for msg in msges {
        out.push((msg.time, msg.channel, msg.sender, msg.content));
    }

    out
}

// TODO: Handle errors
#[tauri::command]
fn send_message(channel: &str, sender: &str, content: &str, hostname: &str, password: &str) {
    let message = client_shared::message::Message::new(sender, content, channel);
    let _ = client_shared::utils::tx_message(hostname, password, 2, message);
}
