//! Terminal UI entry points.

pub mod app;
pub mod board_view;
pub mod config_screen;
pub mod filelist;
pub mod gamemode;
pub mod menu;
pub mod mode_select;
pub mod panels;
pub mod picker;
pub mod play_screen;
pub mod playback;
pub mod replay_screen;
pub mod timeline;

use std::io;

/// Launches the main menu.
pub fn run() -> io::Result<()> {
    let config = crate::config::Config::load();
    let mut terminal = ratatui::init();
    let result = app::run_menu(&mut terminal, &config);
    ratatui::restore();
    result
}

/// Launches directly into the replay viewer for `path`.
pub fn run_replay(path: &str) -> io::Result<()> {
    let config = crate::config::Config::load();
    let replay = match crate::replay::load_recording(std::path::Path::new(path)) {
        Ok(r) => r,
        Err(e) => {
            eprintln!("error: {e}");
            return Ok(());
        }
    };
    if !gamemode::is_supported(&replay.gamemode) {
        eprintln!("{}", gamemode::unsupported_message(&replay.gamemode));
        return Ok(());
    }
    let mut terminal = ratatui::init();
    let result = replay_screen::run(&mut terminal, &replay, &config.palette);
    ratatui::restore();
    result.map(|_quit| ())
}
