//! Main event loop for the navigation TUI

use std::io;
use std::path::Path;
use std::time::Duration;

use crossterm::event::{self, Event};
use ratatui::DefaultTerminal;

use super::keys::handle_key;
use super::state::NavState;
use super::ui::render;
use diaryx_core::config::Config;

use crate::cli::CliWorkspace;
use crate::editor::launch_editor;

/// Run the navigation TUI event loop
pub fn run(
    terminal: &mut DefaultTerminal,
    state: &mut NavState,
    config: &Config,
    ws: &CliWorkspace,
) -> io::Result<()> {
    // Initial preview load
    state.update_preview();

    loop {
        // Clear expired status messages
        state.clear_expired_status();

        // Draw UI
        terminal.draw(|frame| render(frame, state))?;

        // Check for quit
        if state.should_quit {
            break;
        }

        // Check for pending editor action
        if let Some(path) = state.pending_edit.take() {
            open_in_editor(terminal, &path, config)?;
            // After editor closes, refresh preview and rebuild tree in case file was modified
            state.rebuild_tree(ws);
            continue;
        }

        // Handle events (with timeout for responsiveness)
        if event::poll(Duration::from_millis(100))?
            && let Event::Key(key) = event::read()?
        {
            handle_key(state, key, ws);
        }
    }

    Ok(())
}

/// Suspend TUI, open file in editor, then resume TUI
fn open_in_editor(terminal: &mut DefaultTerminal, path: &Path, config: &Config) -> io::Result<()> {
    // Restore terminal to normal mode
    ratatui::restore();

    // Launch editor (this blocks until editor closes)
    if let Err(e) = launch_editor(path, config) {
        eprintln!("Error opening editor: {}", e);
    }

    // Re-initialize terminal for TUI
    *terminal = ratatui::init();

    Ok(())
}
