//! Global Hotkey listener.
//!
//! Uses `rdev` to capture global PTT events across the OS without requiring window focus.

#![forbid(unsafe_code)]
#![deny(clippy::all, clippy::pedantic)]
#![deny(clippy::unwrap_used, clippy::expect_used)]

use rdev::{Event, EventType, Key, listen};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use tracing::info;

/// Spawns the global hotkey listener.
pub fn spawn_hotkey_listener(ptt_flag: Arc<AtomicBool>) {
    thread::spawn(move || {
        info!("Hotkey listener started. Press 'V' to talk.");

        let callback = move |event: Event| match event.event_type {
            EventType::KeyPress(Key::KeyV) => {
                ptt_flag.store(true, Ordering::Relaxed);
            }
            EventType::KeyRelease(Key::KeyV) => {
                ptt_flag.store(false, Ordering::Relaxed);
            }
            _ => {}
        };

        if let Err(e) = listen(callback) {
            tracing::error!("Error listening to global hotkeys: {:?}", e);
        }
    });
}
