use crate::*;

pub struct Track<'a> {
    state: &'a osc::Track,
    selected: bool,
}

impl<'a> Track<'a> {
    pub fn new(state: &'a osc::Track, selected: bool) -> Self {
        Self { state, selected }
    }

    fn get_colors(&self) -> (egui::Color32, egui::Color32) {
        // Returns (background_color, text_color)
        let (dark, bright) = match self.state.state {
            osc::TrackState::Empty => (
                egui::Color32::from_rgb(26, 26, 26),    // #1a1a1a
                egui::Color32::from_rgb(102, 102, 102), // #666666
            ),
            osc::TrackState::Idle => (
                egui::Color32::from_rgb(10, 48, 32),  // #0a3020
                egui::Color32::from_rgb(0, 170, 102), // #00aa66
            ),
            osc::TrackState::Playing => (
                egui::Color32::from_rgb(0, 42, 64),   // #002a40
                egui::Color32::from_rgb(0, 136, 204), // #0088cc
            ),
            osc::TrackState::Recording => (
                egui::Color32::from_rgb(68, 10, 10),  // #440a0a
                egui::Color32::from_rgb(204, 68, 68), // #cc4444
            ),
        };
        if self.selected {
            (bright, dark)
        } else {
            (dark, bright)
        }
    }
}

impl<'a> egui::Widget for Track<'a> {
    fn ui(self, ui: &mut egui::Ui) -> egui::Response {
        let sf = ui.available_width() / 100.0;
        let text_style = egui::TextStyle::Body;
        let mut font_id = ui.style().text_styles.get(&text_style).unwrap().clone();
        font_id.size = 20.0 * sf;

        let (bg_color, fg_color) = self.get_colors();

        let frame_response = egui::Frame::none()
            .fill(bg_color)
            .rounding(egui::Rounding::same(4.0 * sf))
            .stroke(egui::Stroke::new(2.5 * sf, fg_color))
            .outer_margin(egui::Margin::same(5.0 * sf))
            .show(ui, |ui| {
                ui.centered_and_justified(|ui| {
                    ui.add(egui::Label::new(
                        egui::RichText::new(self.state.state.display_str())
                            .font(font_id)
                            .color(fg_color),
                    ));
                });
            });

        // Draw volume bar overlay at the bottom
        let volume_bar_height = 2.0 * sf;
        let margin = 4.0 * sf;
        let frame_rect = frame_response.response.rect;

        // Calculate volume bar position (bottom of the frame, inside the margins)
        let inner_rect = frame_rect.shrink(5.0 * sf); // Match the outer_margin
        let volume_bar_rect = egui::Rect::from_min_max(
            egui::pos2(
                inner_rect.min.x + margin,
                inner_rect.max.y - volume_bar_height - margin,
            ),
            egui::pos2(inner_rect.max.x - margin, inner_rect.max.y - margin),
        );

        // Draw volume bar background
        ui.painter().rect_filled(
            volume_bar_rect,
            egui::Rounding::same(1.0 * sf),
            egui::Color32::from_rgb(51, 51, 51), // #333333
        );

        // Draw volume bar fill (only if volume > 0)
        if self.state.volume > 0.0 {
            let fill_width = volume_bar_rect.width() * self.state.volume;
            if fill_width > 0.0 {
                let fill_rect = egui::Rect::from_min_size(
                    volume_bar_rect.min,
                    egui::vec2(fill_width, volume_bar_rect.height()),
                );

                ui.painter().rect_filled(
                    fill_rect,
                    egui::Rounding::same(1.0 * sf),
                    egui::Color32::from_rgb(0, 255, 136), // #00ff88
                );
            }
        }

        frame_response.response
    }
}
