use anyhow::Result;
use std::path::PathBuf;
use std::fs::File;
use crate::reader::EventReader;

pub fn run(file: PathBuf, verbose: bool) -> Result<()> {
    let f = File::open(&file)?;
    let f = std::io::BufReader::new(f);
    let mut reader = EventReader::new(f);

    let mut event_num = 0;
    while let Some(event) = reader.read_event()? {
        match &event {
            crate::reader::Event::Delay(ms) => {
                println!("{}: {} ({}ms)", event_num, event.name(), ms);
            }
            crate::reader::Event::Noop | crate::reader::Event::ClientToServerEof | crate::reader::Event::ServerToClientEof => {
                println!("{}: {}", event_num, event.name());
            }
            crate::reader::Event::ClientToServer(data) | crate::reader::Event::ServerToClient(data) => {
                println!("{}: {} ({} bytes)", event_num, event.name(), data.len());
                if verbose {
                    let hex = hex::encode(data);
                    println!("   {}", hex);
                }
            }
        }
        event_num += 1;
    }

    Ok(())
}
