//! OSTP GUI - Windows Client Interface
//!
//! Communicates with ostp-daemon via Named Pipe

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

mod ipc;
mod state;

use tauri::Manager;
use state::AppState;

#[tauri::command]
async fn connect_vpn(state: tauri::State<'_, AppState>) -> Result<String, String> {
    ipc::send_command("CONNECT")
        .await
        .map_err(|e| e.to_string())
}

#[tauri::command]
async fn disconnect_vpn(state: tauri::State<'_, AppState>) -> Result<String, String> {
    ipc::send_command("DISCONNECT")
        .await
        .map_err(|e| e.to_string())
}

#[tauri::command]
async fn get_status(state: tauri::State<'_, AppState>) -> Result<String, String> {
    ipc::send_command("STATUS")
        .await
        .map_err(|e| e.to_string())
}

#[tauri::command]
async fn fetch_servers() -> Result<Vec<String>, String> {
    // TODO: Fetch from Master Node API
    Ok(vec![
        "RU - Moscow".to_string(),
        "US - New York".to_string(),
        "DE - Frankfurt".to_string(),
    ])
}

fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_shell::init())
        .manage(AppState::new())
        .invoke_handler(tauri::generate_handler![
            connect_vpn,
            disconnect_vpn,
            get_status,
            fetch_servers
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
