/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/

use anyhow::{Result, anyhow};
use jellyremuxer::demuxers::create_demuxer_autodetect;
use std::{
    env::{args, var},
    fs::File,
};

fn main() -> Result<()> {
    env_logger::init_from_env("LOG");
    let path = args().nth(1).ok_or(anyhow!("first arg is input path"))?;
    let file = File::open(path)?;
    let mut reader =
        create_demuxer_autodetect(Box::new(file))?.ok_or(anyhow!("unknown file type"))?;

    println!("INFO: {:#?}", reader.info()?);
    println!("TRACKS: {:?}", reader.tracks()?);
    println!("CHAPTERS: {:#?}", reader.chapters()?);
    println!("TAGS: {:#?}", reader.tags()?);

    if var("CLUSTERS").is_ok() {
        reader.seek_cluster(None)?;
        while let Some((off, cluster)) = reader.read_cluster()? {
            eprintln!(
                "off={off} num_blocks={}",
                cluster.simple_blocks.len() + cluster.block_groups.len()
            )
        }
    }

    Ok(())
}
