//! Yes/No confirmation dialog widget.

use crate::theme;
use ratatui::layout::{Alignment, Constraint, Layout, Rect};
use ratatui::style::Style;
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use ratatui::Frame;

use super::ModalFrame;

/// Modal dialog with Yes/No buttons.
pub struct ConfirmModal<'a> {
    title: &'a str,
    message: &'a str,
    selected: bool,
}

impl<'a> ConfirmModal<'a> {
    pub fn new(title: &'a str, message: &'a str, selected: bool) -> Self {
        Self {
            title,
            message,
            selected,
        }
    }

    pub fn render_centered(self, frame: &mut Frame, term: Rect) -> Rect {
        let t = theme::get();
        let inner = ModalFrame::new(self.title)
            .width(30)
            .height(5)
            .border_color(t.confirm.border)
            .render_centered(frame, term);

        let rows = Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).split(inner);

        frame.render_widget(
            Paragraph::new(self.message).alignment(Alignment::Center),
            rows[0],
        );

        let yes_style = if self.selected {
            Style::new().fg(t.confirm.button_selected_fg).bg(t.confirm.button_selected_bg)
        } else {
            Style::default()
        };
        let no_style = if !self.selected {
            Style::new().fg(t.confirm.button_selected_fg).bg(t.confirm.button_selected_bg)
        } else {
            Style::default()
        };

        let buttons = Line::from(vec![
            Span::styled(" Yes ", yes_style),
            Span::raw("  "),
            Span::styled(" No ", no_style),
        ]);

        frame.render_widget(
            Paragraph::new(buttons).alignment(Alignment::Center),
            rows[1],
        );

        inner
    }
}
