haj 0.3.7

the beautiful, cross-distro package manager and tui.
use super::{App, centered_rect};
use ratatui::{
    Frame,
    layout::{Constraint, Direction, Layout},
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, LineGauge, Paragraph, Wrap},
};

pub fn render_popup(f: &mut Frame, app: &App) {
    if !app.is_installing {
        return;
    }

    let area = centered_rect(55, 35, f.area());
    f.render_widget(Clear, area);

    let (title, border_color) = if app.tx_failed {
        (" command failed [esc/enter/q to close] ", app.theme.error)
    } else {
        (" active command [q to abort] ", app.theme.accent)
    };
    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(border_color));

    let inner_area = block.inner(area);
    f.render_widget(block, area);

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .margin(1)
        .constraints(
            [
                Constraint::Length(1), // action text
                Constraint::Length(1), // blank spacer
                Constraint::Length(1), // thin loading bar
                Constraint::Length(1), // blank spacer
                Constraint::Min(0),    // live logs
            ]
            .as_ref(),
        )
        .split(inner_area);

    let spinner_chars = crate::ui::SHARK_FRAMES;
    let time_ms = std::time::SystemTime::now()
        .duration_since(std::time::SystemTime::UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis();
    let spinner_idx = ((time_ms / 120) % spinner_chars.len() as u128) as usize;
    let spinner = spinner_chars[spinner_idx];

    let action_text = Paragraph::new(Line::from(vec![
        Span::styled(spinner, Style::default().fg(app.theme.accent)),
        Span::raw(" "),
        Span::raw(&app.current_action),
    ]));
    f.render_widget(action_text, chunks[0]);

    if app.tx_failed {
        let err_bar = Paragraph::new(Line::from(Span::styled(
            " ✗ command failed — review log below and press esc, enter, or q to close",
            Style::default().fg(app.theme.error),
        )));
        f.render_widget(err_bar, chunks[2]);
    } else if app.progress == 0 {
        let width = (chunks[2].width as usize).max(4);
        let pos = ((time_ms / 60) % width as u128) as usize;
        let mut chars = vec!['─'; width];
        for i in 0..4 {
            let idx = (pos + i) % width;
            chars[idx] = '━';
        }
        let anim_str: String = chars.into_iter().collect();

        let anim_text = Paragraph::new(Line::from(vec![Span::styled(
            anim_str,
            Style::default()
                .fg(app.theme.accent)
                .add_modifier(Modifier::BOLD),
        )]));
        f.render_widget(anim_text, chunks[2]);
    } else {
        let ratio = f64::from(app.progress).clamp(0.0, 100.0) / 100.0;
        let gauge = LineGauge::default()
            .filled_style(
                Style::default()
                    .fg(app.theme.accent)
                    .add_modifier(Modifier::BOLD),
            )
            .filled_symbol("━")
            .unfilled_symbol("─")
            .ratio(ratio);
        f.render_widget(gauge, chunks[2]);
    }

    let log_lines: Vec<Line> = app
        .transaction_logs
        .iter()
        .map(|log| Line::from(log.clone()))
        .collect();

    let logs_paragraph = Paragraph::new(log_lines)
        .style(Style::default().fg(app.theme.muted))
        .wrap(Wrap { trim: false })
        .block(Block::default().borders(Borders::NONE));

    f.render_widget(logs_paragraph, chunks[4]);
}

pub fn render_confirm_popup(f: &mut Frame, app: &App) {
    if !app.show_prompt {
        return;
    }

    let area = centered_rect(40, 20, f.area());
    f.render_widget(Clear, area);

    let action_color = if app.prompt_type == "install" {
        app.theme.success
    } else {
        app.theme.error
    };

    let block = Block::default()
        .title(" confirm transaction ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(action_color));

    let text = vec![
        Line::from(""),
        Line::from(vec![
            Span::raw(format!("proceed to {} ", app.prompt_type)),
            Span::styled(
                format!("{}", app.prompt_targets.len()),
                Style::default().add_modifier(Modifier::BOLD),
            ),
            Span::raw(" package(s)?"),
        ]),
        Line::from(""),
        Line::from(Span::styled(
            " [Y]es   [N]o ",
            Style::default().fg(app.theme.muted),
        )),
    ];

    let paragraph = Paragraph::new(text)
        .block(block)
        .alignment(ratatui::layout::Alignment::Center);

    f.render_widget(paragraph, area);
}