// SPDX-License-Identifier: GPL-3.0-or-later

use gtk4::prelude::*;
use gtk4::{self as gtk, Orientation};

/// Build a brightness slider row widget.
///
/// Returns (container, adjustment) where:
/// - container: the GTK box to append to a parent
/// - adjustment: the adjustment to read/set the brightness value
///
/// # Arguments
/// * `label_text` - Display name shown above the slider
/// * `initial_value` - Initial brightness percentage (0-100)
/// * `min_value` - Minimum brightness (for caps)
/// * `max_value` - Maximum brightness (for caps)
pub fn build_brightness_slider(
    label_text: &str,
    initial_value: f64,
    min_value: f64,
    max_value: f64,
) -> (gtk::Box, gtk::Adjustment) {
    let container = gtk::Box::new(Orientation::Vertical, 4);

    // Label row: name + optional remove button slot
    let label_row = gtk::Box::new(Orientation::Horizontal, 8);
    let label = gtk::Label::new(Some(label_text));
    label.set_halign(gtk::Align::Start);
    label.set_hexpand(true);
    label.add_css_class("heading");
    label_row.append(&label);

    container.append(&label_row);

    // Slider + percentage label
    let slider_row = gtk::Box::new(Orientation::Horizontal, 8);
    let adjustment = gtk::Adjustment::new(
        initial_value.clamp(min_value, max_value),
        min_value,
        max_value,
        1.0,
        10.0,
        0.0,
    );

    let scale = gtk::Scale::builder()
        .orientation(Orientation::Horizontal)
        .adjustment(&adjustment)
        .hexpand(true)
        .draw_value(false)
        .build();

    let pct_label = gtk::Label::new(Some(&format!("{:.0}%", initial_value)));
    pct_label.set_width_chars(5);

    let pct_clone = pct_label.clone();
    adjustment.connect_value_changed(move |adj| {
        pct_clone.set_text(&format!("{:.0}%", adj.value()));
    });

    slider_row.append(&scale);
    slider_row.append(&pct_label);
    container.append(&slider_row);

    let sep = gtk::Separator::new(Orientation::Horizontal);
    container.append(&sep);

    (container, adjustment)
}
