use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use ratatui::Frame;

use crate::tui::theme;

const SHIELD: &[&str] = &[
    "                d####b",
    "             d##########b",
    "",
    "          d################b",
    "      d#######################b",
    "    d###########^'''''^##########b",
    "   d##########b        d##########b",
    "  d##########b          d##########b",
    "  ############b        d############",
    "  ##############b....d##############",
    "  ##################################",
    "  ##################################",
    "  ##################################",
    "  ##################################",
    "   ################################",
    "",
    "   ################################",
    "  ##################################",
    "  ##################################",
    "  ##################################",
    "  ##################################",
    "   ################################",
];

const LOGO: &[&str] = &[
    "   _",
    "  | |__ _ __ __ __",
    r"  | '_ \ '_ \\ \ / /",
    "  | |_) | |_) |> <",
    r"  |_.__/| .__//_/\_\",
    "       |_|",
];

const TIPS: &[&str] = &[
    "protip: / to search, ? for keys, q to rage quit",
    "remember: green means go, red means buy the dip",
    "loading your financial destiny...",
    "warming up the order book...",
    "not financial advice (but you knew that)",
    "packing your backpack...",
    "calibrating moon trajectory...",
];

pub fn render(f: &mut Frame, area: Rect) {
    let bg = Paragraph::new("").style(ratatui::style::Style::default().bg(theme::BG));
    f.render_widget(bg, area);

    let content_height = (SHIELD.len() + LOGO.len() + 5) as u16;
    let v = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Min(0),
            Constraint::Length(content_height),
            Constraint::Min(0),
        ])
        .split(area);

    let center = v[1];

    let mut lines: Vec<Line> = Vec::new();

    for row in SHIELD {
        lines.push(Line::from(Span::styled(*row, theme::highlight())));
    }

    lines.push(Line::from(""));

    for row in LOGO {
        lines.push(Line::from(Span::styled(*row, theme::highlight())));
    }

    lines.push(Line::from(""));

    // Pick a tip based on current time (changes each second)
    let tip_index = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs() as usize
        % TIPS.len();
    lines.push(Line::from(Span::styled(
        format!("  {}", TIPS[tip_index]),
        theme::dim(),
    )));

    lines.push(Line::from(""));
    lines.push(Line::from(Span::styled(
        format!("  v{}", env!("CARGO_PKG_VERSION")),
        theme::dim(),
    )));

    let paragraph = Paragraph::new(lines).alignment(Alignment::Left);
    f.render_widget(paragraph, center);
}
