use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct LyricSpan {
    pub start_us: u32,
    pub vocals: String,
}

impl LyricSpan {
    #[must_use]
    pub fn span_end_us(&self, lines: &[LyricSpan], line_end: u32) -> u32 {
        let pos = lines
            .iter()
            .position(|l| std::ptr::eq(self, l))
            .expect("self (LyricSpan) is not contained in the slice");

        lines.get(pos + 1).map_or(line_end, |l| l.start_us)
    }

    pub(crate) fn timestamp_string(&self) -> String {
        let mm = self.start_us / 60_000_000;
        let ss = (self.start_us / 1_000_000) % 60;
        let ms = (self.start_us / 10_000) % 100;

        format!("<{mm:02}:{ss:02}.{ms:02}>")
    }
}
