//! Does the container hold an X-mirrored copy of a resource's decoded buffer?
//!
//! The twin invariant (see `twin_mirror_audit`) flags `…_01`/`…_02` pairs that
//! decode to unrelated geometry. If the mirror of one twin's buffer exists
//! somewhere else in the container, that offset is where the other twin belongs
//! and the pair is a mis-anchor; if it does not exist, the pair is simply not a
//! mirrored pair.
//!
//! Usage: find_mirror <container.xpr> <resource>...
use sylpheed_formats::mesh::Xbg7Model;
use std::collections::HashSet;

fn main() {
    let a: Vec<String> = std::env::args().collect();
    let bytes = std::fs::read(&a[1]).expect("container");
    let want: HashSet<String> = a[2..].iter().cloned().collect();
    let models = Xbg7Model::models_named(&bytes, &want, &|| false);
    let be = |at: usize| f32::from_be_bytes(bytes[at..at + 4].try_into().unwrap());

    for m in &models {
        let pos: Vec<[f32; 3]> = m.meshes.iter().flat_map(|s| s.positions.clone()).take(8).collect();
        if pos.len() < 8 {
            continue;
        }
        let anchored = m.meshes[0].vbuf_offset.unwrap_or(0);
        let (mut direct, mut mirror) = (Vec::new(), Vec::new());
        for o in (0..bytes.len().saturating_sub(12 + 8 * 24)).step_by(4) {
            for (flip, out) in [(1.0f32, &mut direct), (-1.0f32, &mut mirror)] {
                if (0..8).all(|k| {
                    let at = o + k * 24;
                    (be(at) - flip * pos[k][0]).abs() <= 1e-4
                        && (be(at + 4) - pos[k][1]).abs() <= 1e-4
                        && (be(at + 8) - pos[k][2]).abs() <= 1e-4
                }) {
                    out.push(o);
                }
            }
        }
        println!(
            "{:<20} anchored 0x{anchored:x}  direct copies {:x?}  mirrored copies {:x?}",
            m.name, direct, mirror
        );
    }
}
