use revelo_core::{StreamCollection, StreamKind};
pub fn to_ebu_core(streams: &StreamCollection, file_path: &str) -> String {
    let mut out = String::new();
    out.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    out.push_str("<ebucore:ebuCoreMain xmlns:ebucore=\"urn:ebu:metadata-schema:ebuCore_2015\">\n");
    out.push_str("<ebucore:coreMetadata>\n");
    out.push_str(&format!(
        "<ebucore:identifier>{}</ebucore:identifier>\n",
        crate::xml_escape(file_path)
    ));
    for kind in [StreamKind::General, StreamKind::Video, StreamKind::Audio] {
        let c = streams.stream_count(kind);
        for p in 0..c {
            if let Some(s) = streams.stream(kind, p) {
                out.push_str(&format!("<ebucore:format type=\"{}\">\n", kind.name()));
                for (k, v) in s.iter() {
                    out.push_str(&format!(
                        "<ebucore:{}>{}</ebucore:{}>\n",
                        k.to_lowercase().replace('_', ""),
                        crate::xml_escape(v.as_str()),
                        k.to_lowercase().replace('_', "")
                    ));
                }
                out.push_str("</ebucore:format>\n");
            }
        }
    }
    out.push_str("</ebucore:coreMetadata>\n</ebucore:ebuCoreMain>\n");
    out
}
#[cfg(test)]
mod tests {
    use super::*;
    use revelo_util::Ztring;
    #[test]
    fn test() {
        let mut c = StreamCollection::new();
        c.set_field(StreamKind::General, 0, "Format", Ztring::from("MP4"));
        let xml = to_ebu_core(&c, "/x.mp4");
        assert!(xml.contains("ebucore:ebuCoreMain"));
        assert!(xml.contains("/x.mp4"));
    }

    #[test]
    fn escapes_xml_special_chars_in_values() {
        let mut c = StreamCollection::new();
        c.set_field(StreamKind::General, 0, "Format", Ztring::from("H.264 & <AAC>"));
        let xml = to_ebu_core(&c, "/a&b<c>.mp4");
        // Raw '&', '<', '>' must not leak into the output.
        assert!(xml.contains("H.264 &amp; &lt;AAC&gt;"));
        assert!(xml.contains("/a&amp;b&lt;c&gt;.mp4"));
        assert!(!xml.contains("& <AAC>"));
    }
}
