/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2024 Mike Robeson [dijksterhuis]
*/
mod helpers;
use helpers::*;
use ot_tools_io::{
    ArrangementFile, BankFile, CalculateChecksum, CheckChecksum, OctatrackFileIO,
    SampleSettingsFile,
};
use std::path::PathBuf;

#[allow(dead_code)]
fn arrangement_check() -> RBoxErr<()> {
    let fp = PathBuf::from("test-data")
        .join("arrange")
        .join("full-options.work");
    let bin = ArrangementFile::from_data_file(&fp)?;
    println!("checksum from arrangement file: {:?}", bin.checksum);

    let checksum = bin.calculate_checksum()?;
    println!("calculated checksum for arrangement file: {checksum:?}");

    if bin.check_checksum()? {
        println!("arrangement file checksum is ok");
    } else {
        println!("bad arrangement file checksum");
        panic!()
    };
    Ok(())
}

fn bank_check() -> RBoxErr<()> {
    let fp = PathBuf::from("test-data")
        .join("blank-project")
        .join("bank01.work");
    let bin = BankFile::from_data_file(&fp)?;
    println!("checksum from bank file: {:?}", bin.checksum);

    let checksum = bin.calculate_checksum()?;
    println!("calculated checksum for bank file: {checksum:?}");

    if bin.check_checksum()? {
        println!("bank file checksum is ok");
    } else {
        println!("bad bank file checksum");
        panic!()
    };
    Ok(())
}

fn markers_check() -> RBoxErr<()> {
    let fp = PathBuf::from("test-data")
        .join("blank-project")
        .join("markers.work");
    let bin = ArrangementFile::from_data_file(&fp)?;
    println!("checksum from markers file: {:?}", bin.checksum);

    let checksum = bin.calculate_checksum()?;
    println!("calculated checksum for markers file: {checksum:?}");

    if bin.check_checksum()? {
        println!("markers file checksum is ok");
    } else {
        println!("bad markers file checksum");
        panic!()
    };

    Ok(())
}

fn sample_check() -> RBoxErr<()> {
    let fp = PathBuf::from("test-data").join("samples").join("sample.ot");
    let bin = SampleSettingsFile::from_data_file(&fp)?;
    println!("checksum from sample settings file: {:?}", bin.checksum);

    let checksum = bin.calculate_checksum()?;
    println!("calculated checksum for sample settings file: {checksum:?}");

    if bin.check_checksum()? {
        println!("sample settings file checksum is ok");
    } else {
        println!("bad sample settings file checksum");
        panic!()
    };
    Ok(())
}

fn main() -> RBoxErr<()> {
    println!("-------------------------");
    // TODO: Arrangement checksums are yet to be reverse engineered
    //       the `calculate_checksum` method will always return 0 until then
    // arrangement_check();
    // println!("-------------------------");
    bank_check()?;
    println!("-------------------------");
    markers_check()?;
    println!("-------------------------");
    sample_check()?;
    println!("-------------------------");
    Ok(())
}
