use crate::theme;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::Color;
use ratatui::widgets::Widget;

const BLOCKS: [char; 8] = ['\u{2581}', '\u{2582}', '\u{2583}', '\u{2584}', '\u{2585}', '\u{2586}', '\u{2587}', '\u{2588}'];

pub struct Spectrum<'a> {
    data: &'a [f32; 32],
}

impl<'a> Spectrum<'a> {
    pub fn new(data: &'a [f32; 32]) -> Self {
        Self { data }
    }
}

impl Widget for Spectrum<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        if area.width == 0 || area.height == 0 {
            return;
        }

        let colors = theme::get();
        let height = area.height as f32;
        let base = area.width as usize / 32;
        let remainder = area.width as usize % 32;
        if base == 0 && remainder == 0 {
            return;
        }

        let mut x_start = area.x;
        for (band, &mag) in self.data.iter().enumerate() {
            let w = base + if band < remainder { 1 } else { 0 };
            if w == 0 {
                continue;
            }
            let bar_height = mag * height;
            let full_cells = bar_height as usize;
            let frac = bar_height - full_cells as f32;
            let frac_idx = (frac * 8.0) as usize;

            for row in 0..area.height as usize {
                let y = area.y + area.height - 1 - row as u16;
                let ratio = row as f32 / area.height as f32;
                let color = if ratio < 0.33 {
                    Color::Rgb(colors.meter.low_rgb.0, colors.meter.low_rgb.1, colors.meter.low_rgb.2)
                } else if ratio < 0.66 {
                    Color::Rgb(colors.meter.mid_rgb.0, colors.meter.mid_rgb.1, colors.meter.mid_rgb.2)
                } else {
                    Color::Rgb(colors.meter.high_rgb.0, colors.meter.high_rgb.1, colors.meter.high_rgb.2)
                };
                for dx in 0..w as u16 {
                    let x = x_start + dx;
                    if row < full_cells {
                        buf[(x, y)].set_char(BLOCKS[7]).set_fg(color);
                    } else if row == full_cells && frac_idx > 0 {
                        buf[(x, y)].set_char(BLOCKS[frac_idx - 1]).set_fg(color);
                    }
                }
            }
            x_start += w as u16;
        }
    }
}
