// src/tui/netrender.rs
use ratatui::{prelude::*, widgets::*};
use ratatui::text::Line;
pub struct AppState {
    pub content_state: ListState,
    pub loading_success: bool,
    pub is_loading: bool,
    // 是否处于输入模式
    pub show_input: bool,
    // 输入框的内容
    pub input_buffer: String,
    // page size for content display
    pub page_size: usize,
    // show title
    pub show_title: bool,
}

pub fn render_ui(
    frame: &mut Frame,
    state: &mut AppState,
    content: &[String],
    title: &str,
    url: &str,
) {
    let constraint = if state.show_input || state.show_title {
        [Constraint::Min(1), Constraint::Length(1)]
    } else {
        [Constraint::Min(1), Constraint::Length(0)]
    };

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints(constraint)
        .split(frame.area());

    // 1. Content Rendering
    let inner_width = chunks[0].width.saturating_sub(7) as usize;
    let items: Vec<ListItem> = content
        .iter()
        .map(|line| {
            let wrapped = textwrap::wrap(line, inner_width);
            let mut lines: Vec<Line> = wrapped
                .into_iter()
                .map(|c| Line::raw(c))
                .collect();
            lines.push(Line::raw(""));
            ListItem::new(lines)
        })
        .collect();

    let blocks = if state.show_title {
        Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .title(format!(" {} ", title))
            .border_style(Style::default().fg(Color::DarkGray))
    } else {
        Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(Style::default().fg(Color::DarkGray))
    };

    let list = List::new(items)
        .block(blocks)
        .highlight_style(
            Style::default()
                // #96CEC1
                .fg(Color::Rgb(150, 206, 193)),
        )
        .highlight_symbol(" > ");

    frame.render_stateful_widget(list, chunks[0], &mut state.content_state);

    // 2. Footer Rendering
    // let side_padding = Layout::default()
    //     .direction(Direction::Horizontal)
    //     .constraints([
    //         Constraint::Length(1),
    //         Constraint::Min(1),
    //         Constraint::Length(1),
    //     ])
    //     .split(chunks[1]);

    // let inner_area = side_padding[1];

    if state.show_input {
        let foot_chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Min(10), Constraint::Length(25)])
            .split(chunks[1]);

        frame.render_widget(
            Paragraph::new(format!("Jump: {}_", state.input_buffer))
                .style(Style::default().fg(Color::White).bold()),
            foot_chunks[0],
        );

        // Footer hints
        frame.render_widget(
            Paragraph::new(" [Enter] Go  [Esc] Cancel")
                .alignment(Alignment::Right)
                .style(Style::default().fg(Color::Gray)),
            foot_chunks[1],
        );
    } else {
        let foot_chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Length(11),
                Constraint::Min(1),
                Constraint::Max(55),
            ])
            .split(chunks[1]);

        let (status_text, status_color) = match state.is_loading {
            true => ("  LOADING  ", Color::Yellow),
            false if state.loading_success => ("  LOADED  ", Color::Green),
            _ => ("  FAILED  ", Color::Red),
        };

        let current_line = state.content_state.selected().map(|i| i + 1).unwrap_or(0);
        let total_lines = content.len();

        frame.render_widget(
            Paragraph::new(status_text)
                .style(Style::default().bg(status_color).fg(Color::White).bold()),
            foot_chunks[0],
        );

        frame.render_widget(
            Paragraph::new(format!(" URL: {} ", url))
                .alignment(Alignment::Left)
                .style(Style::default().fg(Color::Gray)),
            foot_chunks[1],
        );

        // Only show full help text if there's enough width
        let help_text = if chunks[1].width > 60 {
            format!(
                "{}/{}  [/] Jump [n] Next [p] Prev [q] Quit [r] Refresh",
                current_line, total_lines
            )
        } else {
            format!("{}/{}", current_line, total_lines)
        };

        frame.render_widget(
            Paragraph::new(help_text)
                .alignment(Alignment::Right)
                .style(Style::default().fg(Color::Gray)),
            foot_chunks[2],
        );
    }
}
