//! Real-disc test for the movie subtitle chain. Skipped when the extracted disc
//! is absent (set `SYLPHEED_DISC` to the extract root to enable).

use std::path::PathBuf;

use sylpheed_formats::movie_subtitle::{self, SubLang};
use sylpheed_formats::PakArchive;

fn disc_root() -> Option<PathBuf> {
    let p = PathBuf::from(std::env::var("SYLPHEED_DISC").ok()?);
    p.join("dat").is_dir().then_some(p)
}

#[test]
fn resolves_english_radio_subtitles() {
    let Some(root) = disc_root() else {
        eprintln!("SKIP: set SYLPHEED_DISC");
        return;
    };
    let lang = SubLang::English;
    let lang_pak = PakArchive::open(root.join(format!("dat/movie/{}.pak", lang.pak_code()))).unwrap();
    let text_pak =
        PakArchive::open(root.join(format!("dat/GP_MAIN_GAME_{}.pak", lang.game_code()))).unwrap();

    // A radio movie: real timed English dialogue.
    let cues = movie_subtitle::load("RT01C_1", &lang_pak, &text_pak);
    assert!(!cues.is_empty(), "RT01C_1 should have timed cues");
    assert!(cues[0].start >= 0.0 && cues[0].start < 5.0);
    assert!(
        cues.iter().any(|c| c.text.contains("Rhino Leader")),
        "expected recognizable dialogue, got: {:?}",
        cues.iter().map(|c| &c.text).collect::<Vec<_>>()
    );

    // No MSG_DEMO keys should leak into resolved caption text.
    assert!(
        cues.iter().all(|c| !c.text.contains("MSG_DEMO")),
        "unresolved key leaked: {:?}",
        cues.iter().map(|c| &c.text).collect::<Vec<_>>()
    );

    // A resupply movie: the canonical resupply line.
    let hokyu = movie_subtitle::load("hokyu_DS_s02A", &lang_pak, &text_pak);
    assert!(hokyu.iter().any(|c| c.text.contains("Resupply complete")));

    // S00A carries inline narration text (not MSG_DEMO refs) with timecodes.
    let story = movie_subtitle::load("S00A", &lang_pak, &text_pak);
    assert!(!story.is_empty() && story.iter().any(|c| c.text.contains("27th century")));

    // S13A stores a two-line caption as two consecutive text tokens sharing one
    // timing; both lines must survive (the "Look at it father" regression — the
    // first line used to be dropped).
    let s13 = movie_subtitle::load("S13A", &lang_pak, &text_pak);
    let multiline = s13
        .iter()
        .find(|c| c.text.contains("Look at it father"))
        .expect("S13A should contain the 'Look at it father' caption");
    assert_eq!(
        multiline.text, "Look at it father\n& beautiful isn't it",
        "both lines of the caption must be present"
    );
    assert!((multiline.start - 74.8).abs() < 0.1, "start {}", multiline.start);
}
