use super::ui_rows_ext::UiRowsExt;

pub struct Column<'a, const ROWS: usize> {
    column: &'a osc::Column<ROWS>,
    selected_row: Option<usize>,
    metronome_position: f32,
}

impl<'a, const ROWS: usize> Column<'a, ROWS> {
    pub fn new(
        column: &'a osc::Column<ROWS>,
        selected_row: Option<usize>,
        metronome_position: f32,
    ) -> Self {
        Self {
            column,
            selected_row,
            metronome_position,
        }
    }
}

impl<'a, const ROWS: usize> egui::Widget for Column<'a, ROWS> {
    fn ui(self, ui: &mut egui::Ui) -> egui::Response {
        let idle = self
            .column
            .tracks
            .iter()
            .all(|t| t.state == osc::TrackState::Idle || t.state == osc::TrackState::Empty);

        let first_recording =
            self.column.tracks.iter().all(|t| {
                t.state == osc::TrackState::Recording || t.state == osc::TrackState::Empty
            });

        let response = ui.vertical(|ui| {
            ui.add(super::PositionIndicator::new(
                idle,
                first_recording,
                self.column.beat_count,
                self.column.current_beat,
                self.metronome_position,
            ));

            ui.rows(self.column.tracks.len(), |ui| {
                for (i, track) in self.column.tracks.iter().enumerate() {
                    ui[i].add(super::Track::new(track, Some(i) == self.selected_row));
                }
            });
        });
        response.response
    }
}
