use crate::theme::palette as theme;
use crate::tui::state::AppState;
use ratatui::{
    layout::{Constraint, Direction, Layout, Rect},
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, BorderType, Borders, Paragraph},
    Frame,
};

pub fn render(f: &mut Frame, area: Rect, state: &AppState) {
    f.render_widget(Block::default().style(Style::default().bg(theme::BG)), area);

    let error_line = if let Some(msg) = &state.status_message {
        Line::from(Span::styled(
            msg.as_str(),
            Style::default().fg(theme::DANGER),
        ))
    } else {
        Line::from("")
    };

    let box_height = 10u16;
    let vert = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Fill(1),
            Constraint::Length(box_height),
            Constraint::Fill(1),
        ])
        .split(area);

    let box_width = area.width.min(70);
    let pad = (area.width.saturating_sub(box_width)) / 2;
    let inner = Rect {
        x: area.x + pad,
        y: vert[1].y,
        width: box_width,
        height: box_height,
    };

    let content = vec![
        Line::from(Span::styled(
            "Add OTP",
            Style::default()
                .fg(theme::TEXT)
                .add_modifier(Modifier::BOLD),
        )),
        Line::from(""),
        Line::from(Span::styled(
            "QR code path, otpauth:// URI, or Base32 secret:",
            Style::default().fg(theme::TEXT_MUTED),
        )),
        Line::from(Span::styled(
            format!("{}_", state.add_secret_input),
            Style::default().fg(theme::TEXT),
        )),
        Line::from(""),
        error_line,
        Line::from(""),
        Line::from(Span::styled(
            "[ Enter ] next   [ Tab ] browse   [ Esc ] cancel",
            Style::default().fg(theme::TEXT_MUTED),
        )),
    ];

    f.render_widget(
        Paragraph::new(content).block(
            Block::default()
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(Style::default().fg(theme::BORDER))
                .style(Style::default().bg(theme::BG)),
        ),
        inner,
    );
}
