//! Input injection module
//!
//! Handles mouse and keyboard input simulation using Windows SendInput API.

mod keyboard;
mod mouse;

pub use keyboard::KeyboardController;
pub use mouse::MouseController;

use anyhow::Result;

/// Combined input controller for mouse and keyboard
pub struct InputController {
    mouse: MouseController,
    keyboard: KeyboardController,
}

impl InputController {
    /// Create a new input controller
    pub fn new() -> Result<Self> {
        Ok(Self {
            mouse: MouseController::new()?,
            keyboard: KeyboardController::new()?,
        })
    }

    /// Get mouse controller
    pub fn mouse(&mut self) -> &mut MouseController {
        &mut self.mouse
    }

    /// Get keyboard controller
    pub fn keyboard(&mut self) -> &mut KeyboardController {
        &mut self.keyboard
    }

    /// Move mouse to absolute position
    pub fn mouse_move(&mut self, x: i32, y: i32) -> Result<()> {
        self.mouse.move_to(x, y)
    }

    /// Click mouse button
    pub fn mouse_click(&mut self, button: MouseButton, down: bool) -> Result<()> {
        if down {
            self.mouse.button_down(button)
        } else {
            self.mouse.button_up(button)
        }
    }

    /// Scroll mouse wheel
    pub fn mouse_scroll(&mut self, delta_x: i32, delta_y: i32) -> Result<()> {
        self.mouse.scroll(delta_x, delta_y)
    }

    /// Press or release a key
    pub fn key_event(&mut self, vk_code: u16, down: bool) -> Result<()> {
        if down {
            self.keyboard.key_down(vk_code)
        } else {
            self.keyboard.key_up(vk_code)
        }
    }

    /// Type a unicode character
    pub fn type_unicode(&mut self, ch: char) -> Result<()> {
        self.keyboard.type_char(ch)
    }

    /// Send Ctrl+Alt+Delete (requires special handling on Windows)
    pub fn send_ctrl_alt_del(&mut self) -> Result<()> {
        self.keyboard.send_sas()
    }
}

/// Mouse button types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MouseButton {
    Left,
    Right,
    Middle,
    X1,
    X2,
}

impl Default for InputController {
    fn default() -> Self {
        Self::new().expect("Failed to create input controller")
    }
}
