/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2024 Mike Robeson [dijksterhuis]
*/

mod helpers;
use helpers::*;
use ot_tools_io::OctatrackFileIO;
#[allow(unused_imports)]
use ot_tools_io::{ArrangementFile, BankFile, MarkersFile, ProjectFile, SampleSettingsFile};

#[test]
// Windows will add on carriage returns...
#[cfg(not(target_os = "windows"))]
fn arrangement_blank() -> RBoxErr<()> {
    let valid_yaml_path = get_arrange_dirpath().join("blank.yaml");
    let binpath = get_arrange_dirpath().join("blank.work");
    let outyaml = std::env::temp_dir().join("serde-ot-bin2yaml-arrange-blank.yaml");

    let r = ArrangementFile::from_data_file(&binpath)?.to_yaml_file(&outyaml);
    print_and_assert_ok_result(r);
    let written = read_str_file(&outyaml)?;
    let valid = read_str_file(&valid_yaml_path)?;

    let _ = std::fs::remove_file(&outyaml);
    assert_eq!(valid, written);
    Ok(())
}

#[test]
// Windows will add on carriage returns...
#[cfg(not(target_os = "windows"))]
fn arrangement_full_options() -> RBoxErr<()> {
    let valid_yaml_path = get_arrange_dirpath().join("full-options.yaml");
    let binpath = get_arrange_dirpath().join("full-options.work");
    let outyaml = std::env::temp_dir().join("serde-ot-bin2yaml-arrange-fulloptions.yaml");

    let r = ArrangementFile::from_data_file(&binpath)?.to_yaml_file(&outyaml);
    print_and_assert_ok_result(r);
    let written = read_str_file(&outyaml).unwrap();
    let valid = read_str_file(&valid_yaml_path).unwrap();

    let _ = std::fs::remove_file(&outyaml);
    assert_eq!(valid, written);
    Ok(())
}

#[test]
// Windows will add on carriage returns...
#[cfg(not(target_os = "windows"))]
fn arrangement_one_blank_reminder_row() -> RBoxErr<()> {
    let valid_yaml_path = get_arrange_dirpath().join("1-rem-blank-txt.yaml");
    let binpath = get_arrange_dirpath().join("1-rem-blank-txt.work");
    let outyaml = std::env::temp_dir().join("serde-ot-bin2yaml-arrange-onereminderrow.yaml");

    let r = ArrangementFile::from_data_file(&binpath)?.to_yaml_file(&outyaml);
    print_and_assert_ok_result(r);
    let written = read_str_file(&outyaml).unwrap();
    let valid = read_str_file(&valid_yaml_path).unwrap();

    let _ = std::fs::remove_file(&outyaml);
    assert_eq!(valid, written);
    Ok(())
}

#[test]
fn test_bank() -> RBoxErr<()> {
    let outfile = std::env::temp_dir().join("ot-tools-actions-bin2yaml-bank-ok.yaml");
    let binfile = get_blank_proj_dirpath().join("bank01.work");
    let r = BankFile::from_data_file(&binfile)?.to_yaml_file(&outfile);
    let _ = std::fs::remove_file(&outfile);
    assert!(r.is_ok());
    Ok(())
}

#[test]
fn test_project() -> RBoxErr<()> {
    let outfile = std::env::temp_dir().join("ot-tools-actions-bin2yaml-project-ok.yaml");
    let binfile = get_blank_proj_dirpath().join("project.work");
    let r = ProjectFile::from_data_file(&binfile)?.to_yaml_file(&outfile);
    let _ = std::fs::remove_file(&outfile);
    assert!(r.is_ok());
    Ok(())
}

#[test]
fn test_sample() -> RBoxErr<()> {
    let outfile = std::env::temp_dir().join("ot-tools-actions-bin2yaml-sample-ok.yaml");
    let binfile = get_samples_dirpath().join("sample.ot");
    let r = SampleSettingsFile::from_data_file(&binfile)?.to_yaml_file(&outfile);
    let _ = std::fs::remove_file(&outfile);
    assert!(r.is_ok());
    Ok(())
}
