use gtk4::prelude::*;
use gtk4::{Box as GtkBox, CheckButton, DrawingArea, Label, Orientation, Separator};
use std::sync::Arc;

use crate::processor::AtomicF32;
use super::knob::Knob;
use super::meter::Meter;

#[derive(Clone)]
pub struct BandStrip {
    pub widget:    GtkBox,
    pub da_vu:     DrawingArea,
    pub da_gr:     DrawingArea,
    pub freq_label: Label,      // actualizable cuando cambia num_bands
    pub threshold: Knob,
    pub ratio:     Knob,
    pub attack:    Knob,
    pub release:   Knob,
    pub makeup:    Knob,
    pub clip_lvl:  Knob,
    pub clip_en:   CheckButton,
    pub enabled:   CheckButton,
}

impl BandStrip {
    pub fn new(
        band_idx:   usize,
        freq_label: &str,
        level_arc:  Arc<AtomicF32>,
        gr_arc:     Arc<AtomicF32>,
    ) -> Self {
        let col = GtkBox::builder()
            .orientation(Orientation::Vertical).spacing(2)
            .hexpand(true)                    // cada banda toma igual ancho
            .css_classes(["band-strip"]).build();

        // Cabecera
        col.append(&Label::builder()
            .label(&format!("B{}", band_idx + 1))
            .css_classes(["band-num"]).build());
        let freq_lbl = Label::builder()
            .label(freq_label).css_classes(["band-freq"]).build();
        col.append(&freq_lbl);

        // Etiquetas metro
        let lbl_row = GtkBox::builder()
            .orientation(Orientation::Horizontal).spacing(4)
            .halign(gtk4::Align::Center).build();
        lbl_row.append(&Label::builder().label("VU").css_classes(["meter-lbl"]).build());
        lbl_row.append(&Label::builder().label("GR").css_classes(["meter-lbl"]).build());
        col.append(&lbl_row);

        // Metros
        let meter_vu = Meter::new_level(level_arc);
        let meter_gr = Meter::new_gr(gr_arc);
        let da_vu = meter_vu.widget.clone();
        let da_gr = meter_gr.widget.clone();
        let m_row = GtkBox::builder()
            .orientation(Orientation::Horizontal).spacing(2)
            .halign(gtk4::Align::Center).build();
        m_row.append(&meter_vu.widget);
        m_row.append(&meter_gr.widget);
        col.append(&m_row);

        col.append(&Separator::new(Orientation::Horizontal));

        // Knobs
        let threshold = Knob::new("THR",  "dB", -60.0, 0.0,    -18.0);
        let ratio     = Knob::new("RATIO", ":1",  1.0, 20.0,     3.0);
        let attack    = Knob::new("ATK",  "ms",  0.1, 200.0,    10.0);
        let release   = Knob::new("REL",  "ms", 10.0, 2000.0,  150.0);
        let makeup    = Knob::new("GAIN", "dB",  0.0,  24.0,     3.0);
        col.append(&threshold.widget);
        col.append(&ratio.widget);
        col.append(&attack.widget);
        col.append(&release.widget);
        col.append(&makeup.widget);

        col.append(&Separator::new(Orientation::Horizontal));

        let clip_en  = CheckButton::builder().label("CLIP").active(true)
            .css_classes(["band-check"]).build();
        let clip_lvl = Knob::new("LVL", "dB", -12.0, 0.0, -0.5);
        col.append(&clip_en);
        col.append(&clip_lvl.widget);

        col.append(&Separator::new(Orientation::Horizontal));

        let enabled = CheckButton::builder().label("ON").active(true)
            .css_classes(["band-check"]).build();
        col.append(&enabled);

        Self {
            widget: col, da_vu, da_gr, freq_label: freq_lbl,
            threshold, ratio, attack, release, makeup, clip_lvl, clip_en, enabled,
        }
    }

    /// Actualiza la etiqueta de rango de frecuencias de esta banda.
    pub fn set_freq_label(&self, text: &str) {
        self.freq_label.set_text(text);
    }

    /// Activa o desactiva visualmente la banda (bandas fuera de rango = apagadas).
    pub fn set_active(&self, active: bool) {
        self.widget.set_sensitive(active);
        self.widget.set_opacity(if active { 1.0 } else { 0.22 });
    }
}
