#![allow(dead_code, unused_variables, clippy::too_many_arguments, clippy::field_reassign_with_default, clippy::explicit_counter_loop, clippy::collapsible_match)]
mod app;
mod games;
mod save;
mod ui;

use std::io;
use std::time::{Duration, Instant};

use app::App;
use crossterm::{
    event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{backend::CrosstermBackend, Terminal};

fn main() -> io::Result<()> {
    // Panic hook — restore terminal on crash
    let default_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |info| {
        let _ = disable_raw_mode();
        let _ = execute!(io::stdout(), LeaveAlternateScreen, DisableMouseCapture);
        default_hook(info);
    }));

    // Terminal setup
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let mut app = App::new();
    let tick_rate = Duration::from_millis(16); // ~60fps

    // Main loop
    loop {
        let frame_start = Instant::now();

        // Draw
        terminal.draw(|f| {
            ui::draw(f, &app);
        })?;

        // Handle input
        let timeout = tick_rate.saturating_sub(frame_start.elapsed());
        if event::poll(timeout)? {
            if let Event::Key(key) = event::read()? {
                match (key.code, key.modifiers) {
                    (KeyCode::Char('q'), _) | (KeyCode::Char('c'), KeyModifiers::CONTROL) => {
                        app.persist();
                        break;
                    }
                    _ => {
                        app.handle_key(key);
                    }
                }
            }
        }

        // Tick (animations, timers)
        app.tick(frame_start.elapsed());

        if app.should_quit {
            app.persist();
            break;
        }
    }

    // Restore terminal
    disable_raw_mode()?;
    execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        DisableMouseCapture
    )?;
    terminal.show_cursor()?;

    Ok(())
}
