use ratatui::layout::Rect;
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use ratatui::Frame;

use crate::components::tab_bar::Tab;
use crate::theme::theme;

/// Render the status bar at the bottom
pub fn render_status_bar(
    frame: &mut Frame,
    area: Rect,
    active_tab: Tab,
    syncing: bool,
    message: Option<&str>,
    config_editing: bool,
    config_dirty: bool,
) {
    let t = theme();

    let mut spans: Vec<Span<'_>> = Vec::new();

    // Context-sensitive key hints
    match active_tab {
        Tab::Plugins => {
            push_key_hint(&mut spans, " f", " filter ");
            push_key_hint(&mut spans, " t", " type ");
            push_key_hint(&mut spans, " o", " sort ");
            push_key_hint(&mut spans, " s", " sync ");
            push_key_hint(&mut spans, " S", " force ");
            push_key_hint(&mut spans, " p", " prune ");
        }
        Tab::Paths => {
            push_key_hint(&mut spans, " f", " filter ");
            push_key_hint(&mut spans, " a", " add path ");
            push_key_hint(&mut spans, " d", " remove ");
        }
        Tab::Config => {
            if config_editing {
                push_key_hint(&mut spans, " j/k", " field ");
                push_key_hint(&mut spans, " Space", " cycle ");
                push_key_hint(&mut spans, " Enter", " edit ");
                push_key_hint(&mut spans, " x", " clear ");
                if config_dirty {
                    push_key_hint(&mut spans, " w", " save* ");
                }
                push_key_hint(&mut spans, " Esc", " back ");
            } else {
                push_key_hint(&mut spans, " Enter", " edit rule ");
                push_key_hint(&mut spans, " e", " rename ");
                push_key_hint(&mut spans, " a", " add ");
                push_key_hint(&mut spans, " d", " delete ");
                if config_dirty {
                    push_key_hint(&mut spans, " w", " save* ");
                }
                push_key_hint(&mut spans, " r", " reload ");
            }
        }
        Tab::Log => {
            push_key_hint(&mut spans, " j/k", " scroll ");
            push_key_hint(&mut spans, " u/d", " page ");
            push_key_hint(&mut spans, " a", " autoscroll ");
            push_key_hint(&mut spans, " f", " filter ");
            push_key_hint(&mut spans, " c", " clear ");
        }
    }

    // Global keys
    spans.push(Span::raw("│"));
    push_key_hint(&mut spans, " Tab", " next tab ");
    push_key_hint(&mut spans, " T", " theme ");
    push_key_hint(&mut spans, " ?", " help ");
    push_key_hint(&mut spans, " q", " quit ");

    // Sync indicator
    if syncing {
        spans.push(Span::raw("│ "));
        spans.push(Span::styled("⟳ syncing...", t.spinner));
    }

    // Status message
    if let Some(msg) = message {
        spans.push(Span::raw(" │ "));
        spans.push(Span::styled(msg.to_string(), t.status_info));
    }

    let status = Paragraph::new(Line::from(spans)).style(t.status_bar);
    frame.render_widget(status, area);
}

fn push_key_hint<'a>(spans: &mut Vec<Span<'a>>, key: &'a str, desc: &'a str) {
    let t = theme();
    spans.push(Span::styled(key, t.status_bar_key));
    spans.push(Span::styled(desc, t.status_bar));
}
