use std::{fs::File, io::BufReader};

use anyhow::Context;
use bgzip::BGZFReader;
use log::info;

pub fn get_reader(path: &str) -> anyhow::Result<Box<dyn std::io::Read>> {
    info!("Reading: {path}");
    let file_type = *path
        .split(".")
        .collect::<Vec<&str>>()
        .last()
        .context(format!("Can't parse {path}"))?;
    assert!(file_type == "gz" || file_type == "vcf" || file_type == "bed");

    let raw_reader: Box<dyn std::io::Read> = Box::new(File::open(path)?);

    match file_type {
        "gz" => {
            let reader = Box::new(BGZFReader::new(raw_reader)?);
            Ok(Box::new(BufReader::new(reader)))
        }
        "vcf" | "bed" => Ok(Box::new(BufReader::new(raw_reader))),
        t => {
            panic!("unknown file type: {}", t)
        }
    }
}
