pub mod actions;
pub mod editing;
pub mod navigation;

use anyhow::Result;

pub use actions::{handle_enter_action, handle_leave_page};
use crossterm::event::KeyEvent;
use editing::handle_editing_input;
pub use editing::handle_editing_input as handle_edit_input;
use navigation::handle_navigation_input;
pub use navigation::handle_navigation_input as handle_nav_input;

use crate::tui::{status as types, status::read_status, utils::bus::Bus};

pub fn handle_input(key: KeyEvent, bus: &Bus) -> Result<()> {
    let editing = read_status(|status| {
        let is_editing = !matches!(
            status.temporarily.input_raw_buffer,
            types::ui::InputRawBuffer::None
        );
        log::info!(
            "🟣 ModbusDashboard handle_input: key={:?}, editing={}",
            key.code,
            is_editing
        );
        Ok(is_editing)
    })?;

    if editing {
        log::info!("🟣 Routing to handle_editing_input");
        handle_editing_input(key, bus)?;
    } else {
        log::info!("🟣 Routing to handle_navigation_input");
        handle_navigation_input(key, bus)?;
    }
    log::info!("🟣 ModbusDashboard handle_input completed");
    Ok(())
}
