//! Modal overlays drawn on top of the canvas (help, errors).

use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::Color;
use ratatui::widgets::{Block, Borders, Clear, Paragraph, Widget, Wrap};

use super::theme::Theme;

const HELP_TEXT: &str = "\
KEYBINDINGS

  h / ←        pan left
  l / →        pan right
  j / ↓        pan down
  k / ↑        pan up
  +            zoom in
  -            zoom out
  ?            toggle this help
  q / Esc      quit (or close this help)
  Ctrl-C       quit
";

pub fn draw_help_overlay(area: Rect, theme: &Theme, buf: &mut Buffer) {
    let w = 42.min(area.width.saturating_sub(2));
    let h = 13.min(area.height.saturating_sub(2));
    if w == 0 || h == 0 {
        return;
    }
    let x = area.x + (area.width - w) / 2;
    let y = area.y + (area.height - h) / 2;
    let modal = Rect::new(x, y, w, h);

    Clear.render(modal, buf);
    let block = Block::default()
        .title(" help ")
        .borders(Borders::ALL)
        .border_style(theme.style(theme.border));
    Paragraph::new(HELP_TEXT)
        .style(theme.style(theme.text))
        .wrap(Wrap { trim: false })
        .block(block)
        .render(modal, buf);
}

/// Stripe along the top of the screen showing the latest reload error.
/// The previous canvas keeps rendering underneath.
pub fn draw_error_banner(area: Rect, msg: &str, theme: &Theme, buf: &mut Buffer) {
    if area.width < 4 {
        return;
    }
    let h = 3.min(area.height);
    let banner = Rect::new(area.x, area.y, area.width, h);
    Clear.render(banner, buf);
    let block = Block::default()
        .title(" reload error ")
        .borders(Borders::ALL)
        .border_style(theme.style(Color::Red));
    Paragraph::new(msg)
        .style(theme.style(Color::Red))
        .wrap(Wrap { trim: true })
        .block(block)
        .render(banner, buf);
}
