use crate::interpolation::Interpolated;

/// Handles pendulum swing direction and position calculation
#[derive(Debug)]
pub struct Pendulum {
    /// Current swing direction (true = left to right, false = right to left)
    going_right: bool,
}

impl Pendulum {
    /// Create a new pendulum starting in the right direction
    pub fn new() -> Self {
        Self { going_right: true }
    }

    /// Update pendulum with interpolated timing data and return current position
    ///
    /// Returns position on rail where 0.0 = left side, 1.0 = right side
    pub fn update(&mut self, interpolated: &Interpolated) -> f32 {
        // Change direction on beat boundary
        if interpolated.beat_boundary {
            self.going_right = !self.going_right;
        }

        // Calculate position based on current direction
        if self.going_right {
            // Left to right: 0.0 -> 0.0, 1.0 -> 1.0
            interpolated.position_in_beat
        } else {
            // Right to left: 0.0 -> 1.0, 1.0 -> 0.0
            1.0 - interpolated.position_in_beat
        }
    }
}

impl Default for Pendulum {
    fn default() -> Self {
        Self::new()
    }
}
