use ratatui::Frame;
use ratatui::layout::{Constraint, Flex, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::symbols::border;
use ratatui::widgets::{Block, Borders};

use crate::ui::theme::Theme;

pub fn centered_rect(area: Rect, width: Constraint, height: Constraint) -> Rect {
    let [area] = Layout::horizontal([width]).flex(Flex::Center).areas(area);
    let [area] = Layout::vertical([height]).flex(Flex::Center).areas(area);
    area
}

// Uses DIM + dark foreground to suppress background borders
// that would otherwise appear adjacent to modal borders.
pub fn render_scrim(frame: &mut Frame) {
    let buf = frame.buffer_mut();
    let area = buf.area;
    buf.set_style(
        area,
        Style::default()
            .fg(Theme::TEXT_MUTED)
            .add_modifier(Modifier::DIM),
    );
}

pub fn modal_block_with_hint(title: String, hint: String) -> Block<'static> {
    modal_block_with_hint_color(title, hint, Theme::MODAL_BORDER)
}

pub fn modal_block_with_hint_color(
    title: String,
    hint: String,
    border_color: Color,
) -> Block<'static> {
    Block::default()
        .title(title)
        .title_style(
            Style::default()
                .fg(Theme::MODAL_TITLE)
                .add_modifier(Modifier::BOLD),
        )
        .title_bottom(hint)
        .borders(Borders::ALL)
        .border_set(border::ROUNDED)
        .border_style(Style::default().fg(border_color))
        .style(Style::default())
}
