use dotenv::dotenv;
use env_logger::Builder;
use env_logger::Target;
use log::LevelFilter;
use std::env;
use std::fs::File;
use std::io;

use ratatui::{backend::CrosstermBackend, Terminal};

use reedy::{
    app::{App, AppResult},
    event::{Event, EventHandler},
    handler::{handle_key_events, handle_mouse_events},
    tui::Tui,
};

#[tokio::main]
async fn main() -> AppResult<()> {
    // Load .env file
    dotenv().ok();

    // Setup logging if in debug mode
    if env::var("REEDY_ENV").unwrap_or_default() == "DEBUG" {
        let log_path = App::get_log_path();
        println!("Log file location: {}", log_path.display());
        let file = File::create(log_path).unwrap();

        Builder::new()
            .target(Target::Pipe(Box::new(file)))
            .filter_level(LevelFilter::Debug)
            .init();
    }

    // Create an application.
    let mut app = App::new().await;

    // Initialize the terminal user interface.
    let backend = CrosstermBackend::new(io::stdout());
    let terminal = Terminal::new(backend)?;
    let events = EventHandler::new(250);
    let mut tui = Tui::new(terminal, events);
    tui.init()?;

    // Start the main loop.
    while app.running {
        // Render the user interface.
        tui.draw(&mut app)?;
        // Handle events.
        match tui.events.next().await? {
            Event::Tick => {
                app.tick();
                // Perform auto-refresh if pending
                app.perform_auto_refresh().await;
            }
            Event::Key(key_event) => handle_key_events(key_event, &mut app).await?,
            Event::Mouse(mouse_event) => handle_mouse_events(mouse_event, &mut app).await?,
            Event::Resize(width, height) => {
                app.terminal_width = width;
                app.terminal_height = height;
            }
        }
    }

    // Exit the user interface.
    tui.exit()?;
    Ok(())
}
