//! File browser popup renderer for the Open File dialog
//!
//! Renders a structured popup above the prompt with:
//! - Navigation shortcuts (parent, root, home)
//! - Sortable column headers (name, size, modified)
//! - File list with metadata
//! - Scrollbar for long lists

use super::scrollbar::{render_scrollbar, ScrollbarColors, ScrollbarState};
use super::status_bar::truncate_path;
use crate::app::file_open::{
    format_modified, format_size, FileOpenSection, FileOpenState, SortMode,
};
use crate::primitives::display_width::str_width;
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Clear, Paragraph};
use ratatui::Frame;
use rust_i18n::t;

/// Renderer for the file browser popup
pub struct FileBrowserRenderer;

impl FileBrowserRenderer {
    /// Render the file browser popup
    ///
    /// # Arguments
    /// * `frame` - The ratatui frame to render to
    /// * `area` - The rectangular area for the popup (above the prompt)
    /// * `state` - The file open dialog state
    /// * `theme` - The active theme for colors
    /// * `hover_target` - Current mouse hover target (for highlighting)
    /// * `keybindings` - Optional keybinding resolver for displaying shortcuts
    ///
    /// # Returns
    /// Information for mouse hit testing (scrollbar area, thumb positions, etc.)
    pub fn render(
        frame: &mut Frame,
        area: Rect,
        state: &FileOpenState,
        theme: &crate::view::theme::Theme,
        hover_target: &Option<crate::app::HoverTarget>,
        keybindings: Option<&crate::input::keybindings::KeybindingResolver>,
    ) -> Option<FileBrowserLayout> {
        if area.height < 5 || area.width < 20 {
            return None;
        }

        // Clear the area behind the popup
        frame.render_widget(Clear, area);

        // Truncate path for title if needed (leave space for borders and padding)
        let max_title_len = (area.width as usize).saturating_sub(4); // 2 for borders, 2 for padding
        let truncated_path = truncate_path(&state.current_dir, max_title_len);
        let title = format!(" {} ", truncated_path.to_string_plain());

        // Create styled title with highlighted [...] if truncated
        let title_line = if truncated_path.truncated {
            Line::from(vec![
                Span::raw(" "),
                Span::styled(
                    truncated_path.prefix.clone(),
                    Style::default().fg(theme.popup_border_fg),
                ),
                Span::styled("/[...]", Style::default().fg(theme.menu_highlight_fg)),
                Span::styled(
                    truncated_path.suffix.clone(),
                    Style::default().fg(theme.popup_border_fg),
                ),
                Span::raw(" "),
            ])
        } else {
            Line::from(title)
        };

        // Create the popup block with border
        let block = Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().fg(theme.popup_border_fg))
            .style(Style::default().bg(theme.popup_bg))
            .title(title_line);

        let inner_area = block.inner(area);
        frame.render_widget(block, area);

        if inner_area.height < 3 || inner_area.width < 10 {
            return None;
        }

        // Layout: Navigation (2-3 rows) | Header (1 row) | File list (remaining) | Scrollbar (1 col)
        let nav_height = 2u16; // Navigation shortcuts section
        let header_height = 1u16;
        let scrollbar_width = 1u16;

        let content_width = inner_area.width.saturating_sub(scrollbar_width);
        let list_height = inner_area.height.saturating_sub(nav_height + header_height);

        // Navigation area
        let nav_area = Rect::new(inner_area.x, inner_area.y, content_width, nav_height);

        // Header area
        let header_area = Rect::new(
            inner_area.x,
            inner_area.y + nav_height,
            content_width,
            header_height,
        );

        // File list area
        let list_area = Rect::new(
            inner_area.x,
            inner_area.y + nav_height + header_height,
            content_width,
            list_height,
        );

        // Scrollbar area
        let scrollbar_area = Rect::new(
            inner_area.x + content_width,
            inner_area.y + nav_height + header_height,
            scrollbar_width,
            list_height,
        );

        // Render each section with hover state
        Self::render_navigation(frame, nav_area, state, theme, hover_target, keybindings);
        Self::render_header(frame, header_area, state, theme, hover_target);
        let visible_rows = Self::render_file_list(frame, list_area, state, theme, hover_target);

        // Render scrollbar with theme colors (hover-aware)
        let scrollbar_state =
            ScrollbarState::new(state.entries.len(), visible_rows, state.scroll_offset);
        let is_scrollbar_hovered = matches!(
            hover_target,
            Some(crate::app::HoverTarget::FileBrowserScrollbar)
        );
        let colors = if is_scrollbar_hovered {
            ScrollbarColors::from_theme_hover(theme)
        } else {
            ScrollbarColors::from_theme(theme)
        };
        let (thumb_start, thumb_end) =
            render_scrollbar(frame, scrollbar_area, &scrollbar_state, &colors);

        Some(FileBrowserLayout {
            popup_area: area,
            nav_area,
            header_area,
            list_area,
            scrollbar_area,
            thumb_start,
            thumb_end,
            visible_rows,
            content_width,
        })
    }

    /// Render navigation shortcuts section with checkboxes on first row
    fn render_navigation(
        frame: &mut Frame,
        area: Rect,
        state: &FileOpenState,
        theme: &crate::view::theme::Theme,
        hover_target: &Option<crate::app::HoverTarget>,
        keybindings: Option<&crate::input::keybindings::KeybindingResolver>,
    ) {
        use crate::app::HoverTarget;

        // Look up keybindings for toggle actions
        let hidden_shortcut = keybindings
            .and_then(|kb| {
                kb.get_keybinding_for_action(
                    &crate::input::keybindings::Action::FileBrowserToggleHidden,
                    crate::input::keybindings::KeyContext::Prompt,
                )
            })
            .unwrap_or_default();

        let encoding_shortcut = keybindings
            .and_then(|kb| {
                kb.get_keybinding_for_action(
                    &crate::input::keybindings::Action::FileBrowserToggleDetectEncoding,
                    crate::input::keybindings::KeyContext::Prompt,
                )
            })
            .unwrap_or_default();

        // First line: "Show Hidden" and "Detect Encoding" checkboxes
        let mut checkbox_spans = Vec::new();

        // Show Hidden checkbox
        let hidden_icon = if state.show_hidden { "☑" } else { "☐" };
        let hidden_label = format!("{} {}", hidden_icon, t!("file_browser.show_hidden"));
        let hidden_shortcut_text = if hidden_shortcut.is_empty() {
            String::new()
        } else {
            format!(" ({})", hidden_shortcut)
        };

        let is_hidden_hovered = matches!(
            hover_target,
            Some(HoverTarget::FileBrowserShowHiddenCheckbox)
        );
        let hidden_style = if is_hidden_hovered {
            Style::default()
                .fg(theme.menu_hover_fg)
                .bg(theme.menu_hover_bg)
        } else if state.show_hidden {
            Style::default()
                .fg(theme.menu_highlight_fg)
                .bg(theme.popup_bg)
        } else {
            Style::default().fg(theme.help_key_fg).bg(theme.popup_bg)
        };
        let hidden_shortcut_style = if is_hidden_hovered {
            Style::default()
                .fg(theme.menu_hover_fg)
                .bg(theme.menu_hover_bg)
        } else {
            Style::default()
                .fg(theme.help_separator_fg)
                .bg(theme.popup_bg)
        };

        checkbox_spans.push(Span::styled(format!(" {}", hidden_label), hidden_style));
        if !hidden_shortcut_text.is_empty() {
            checkbox_spans.push(Span::styled(hidden_shortcut_text, hidden_shortcut_style));
        }

        // Separator between checkboxes
        checkbox_spans.push(Span::styled(
            " │ ",
            Style::default()
                .fg(theme.help_separator_fg)
                .bg(theme.popup_bg),
        ));

        // Detect Encoding checkbox with underlined E
        let encoding_icon = if state.detect_encoding { "☑" } else { "☐" };
        let is_encoding_hovered = matches!(
            hover_target,
            Some(HoverTarget::FileBrowserDetectEncodingCheckbox)
        );
        let encoding_style = if is_encoding_hovered {
            Style::default()
                .fg(theme.menu_hover_fg)
                .bg(theme.menu_hover_bg)
        } else if state.detect_encoding {
            Style::default()
                .fg(theme.menu_highlight_fg)
                .bg(theme.popup_bg)
        } else {
            Style::default().fg(theme.help_key_fg).bg(theme.popup_bg)
        };
        let encoding_underline_style = if is_encoding_hovered {
            Style::default()
                .fg(theme.menu_hover_fg)
                .bg(theme.menu_hover_bg)
                .add_modifier(Modifier::UNDERLINED)
        } else if state.detect_encoding {
            Style::default()
                .fg(theme.menu_highlight_fg)
                .bg(theme.popup_bg)
                .add_modifier(Modifier::UNDERLINED)
        } else {
            Style::default()
                .fg(theme.help_key_fg)
                .bg(theme.popup_bg)
                .add_modifier(Modifier::UNDERLINED)
        };
        let encoding_shortcut_style = if is_encoding_hovered {
            Style::default()
                .fg(theme.menu_hover_fg)
                .bg(theme.menu_hover_bg)
        } else {
            Style::default()
                .fg(theme.help_separator_fg)
                .bg(theme.popup_bg)
        };

        // "☐ Detect " + "E" (underlined) + "ncoding"
        checkbox_spans.push(Span::styled(
            format!("{} Detect ", encoding_icon),
            encoding_style,
        ));
        checkbox_spans.push(Span::styled("E", encoding_underline_style));
        checkbox_spans.push(Span::styled("ncoding", encoding_style));

        if !encoding_shortcut.is_empty() {
            checkbox_spans.push(Span::styled(
                format!(" ({})", encoding_shortcut),
                encoding_shortcut_style,
            ));
        }

        // Fill rest of row with background
        let checkbox_line_width: usize = checkbox_spans.iter().map(|s| str_width(&s.content)).sum();
        let remaining = (area.width as usize).saturating_sub(checkbox_line_width);
        if remaining > 0 {
            checkbox_spans.push(Span::styled(
                " ".repeat(remaining),
                Style::default().bg(theme.popup_bg),
            ));
        }
        let checkbox_line = Line::from(checkbox_spans);

        // Second line: Navigation shortcuts
        let is_nav_active = state.active_section == FileOpenSection::Navigation;

        let mut nav_spans = Vec::new();
        nav_spans.push(Span::styled(
            format!(" {}", t!("file_browser.navigation")),
            Style::default()
                .fg(theme.help_separator_fg)
                .bg(theme.popup_bg),
        ));

        for (idx, shortcut) in state.shortcuts.iter().enumerate() {
            let is_selected = is_nav_active && idx == state.selected_shortcut;
            let is_hovered =
                matches!(hover_target, Some(HoverTarget::FileBrowserNavShortcut(i)) if *i == idx);

            let style = if is_selected {
                Style::default()
                    .fg(theme.popup_text_fg)
                    .bg(theme.suggestion_selected_bg)
                    .add_modifier(Modifier::BOLD)
            } else if is_hovered {
                Style::default()
                    .fg(theme.menu_hover_fg)
                    .bg(theme.menu_hover_bg)
            } else {
                Style::default().fg(theme.help_key_fg).bg(theme.popup_bg)
            };

            nav_spans.push(Span::styled(format!(" {} ", shortcut.label), style));

            if idx < state.shortcuts.len() - 1 {
                nav_spans.push(Span::styled(
                    " │ ",
                    Style::default()
                        .fg(theme.help_separator_fg)
                        .bg(theme.popup_bg),
                ));
            }
        }

        // Fill rest of navigation row with background
        let nav_line_width: usize = nav_spans.iter().map(|s| str_width(&s.content)).sum();
        let nav_remaining = (area.width as usize).saturating_sub(nav_line_width);
        if nav_remaining > 0 {
            nav_spans.push(Span::styled(
                " ".repeat(nav_remaining),
                Style::default().bg(theme.popup_bg),
            ));
        }
        let nav_line = Line::from(nav_spans);

        let paragraph = Paragraph::new(vec![checkbox_line, nav_line]);
        frame.render_widget(paragraph, area);
    }

    /// Render sortable column headers
    fn render_header(
        frame: &mut Frame,
        area: Rect,
        state: &FileOpenState,
        theme: &crate::view::theme::Theme,
        hover_target: &Option<crate::app::HoverTarget>,
    ) {
        use crate::app::HoverTarget;

        let width = area.width as usize;

        // Column widths
        let size_col_width = 10;
        let date_col_width = 14;
        let name_col_width = width.saturating_sub(size_col_width + date_col_width + 4);

        let header_style = Style::default()
            .fg(theme.help_key_fg)
            .bg(theme.menu_dropdown_bg)
            .add_modifier(Modifier::BOLD);

        let active_header_style = Style::default()
            .fg(theme.menu_highlight_fg)
            .bg(theme.menu_dropdown_bg)
            .add_modifier(Modifier::BOLD);

        let hover_header_style = Style::default()
            .fg(theme.menu_hover_fg)
            .bg(theme.menu_hover_bg)
            .add_modifier(Modifier::BOLD);

        // Sort indicator
        let sort_arrow = if state.sort_ascending { "▲" } else { "▼" };

        let mut spans = Vec::new();

        // Name column
        let name_header = format!(
            " {}{}",
            t!("file_browser.name"),
            if state.sort_mode == SortMode::Name {
                sort_arrow
            } else {
                " "
            }
        );
        let is_name_hovered = matches!(
            hover_target,
            Some(HoverTarget::FileBrowserHeader(SortMode::Name))
        );
        let name_style = if state.sort_mode == SortMode::Name {
            active_header_style
        } else if is_name_hovered {
            hover_header_style
        } else {
            header_style
        };
        let name_display = if name_header.len() < name_col_width {
            format!("{:<width$}", name_header, width = name_col_width)
        } else {
            name_header[..name_col_width].to_string()
        };
        spans.push(Span::styled(name_display, name_style));

        // Size column
        let size_header = format!(
            "{:>width$}",
            format!(
                "{}{}",
                t!("file_browser.size"),
                if state.sort_mode == SortMode::Size {
                    sort_arrow
                } else {
                    " "
                }
            ),
            width = size_col_width
        );
        let is_size_hovered = matches!(
            hover_target,
            Some(HoverTarget::FileBrowserHeader(SortMode::Size))
        );
        let size_style = if state.sort_mode == SortMode::Size {
            active_header_style
        } else if is_size_hovered {
            hover_header_style
        } else {
            header_style
        };
        spans.push(Span::styled(size_header, size_style));

        // Separator
        spans.push(Span::styled("  ", header_style));

        // Modified column
        let modified_header = format!(
            "{:>width$}",
            format!(
                "{}{}",
                t!("file_browser.modified"),
                if state.sort_mode == SortMode::Modified {
                    sort_arrow
                } else {
                    " "
                }
            ),
            width = date_col_width
        );
        let is_modified_hovered = matches!(
            hover_target,
            Some(HoverTarget::FileBrowserHeader(SortMode::Modified))
        );
        let modified_style = if state.sort_mode == SortMode::Modified {
            active_header_style
        } else if is_modified_hovered {
            hover_header_style
        } else {
            header_style
        };
        spans.push(Span::styled(modified_header, modified_style));

        let line = Line::from(spans);
        let paragraph = Paragraph::new(vec![line]);
        frame.render_widget(paragraph, area);
    }

    /// Render the file list with metadata columns
    ///
    /// Returns the number of visible rows
    fn render_file_list(
        frame: &mut Frame,
        area: Rect,
        state: &FileOpenState,
        theme: &crate::view::theme::Theme,
        hover_target: &Option<crate::app::HoverTarget>,
    ) -> usize {
        use crate::app::HoverTarget;

        let visible_rows = area.height as usize;
        let width = area.width as usize;

        // Column widths (matching header)
        let size_col_width = 10;
        let date_col_width = 14;
        let name_col_width = width.saturating_sub(size_col_width + date_col_width + 4);

        let is_files_active = state.active_section == FileOpenSection::Files;

        // Loading state
        if state.loading {
            let loading_line = Line::from(Span::styled(
                t!("file_browser.loading").to_string(),
                Style::default()
                    .fg(theme.help_separator_fg)
                    .bg(theme.popup_bg),
            ));
            let paragraph = Paragraph::new(vec![loading_line]);
            frame.render_widget(paragraph, area);
            return visible_rows;
        }

        // Error state
        if let Some(error) = &state.error {
            let error_line = Line::from(Span::styled(
                t!("file_browser.error", error = error).to_string(),
                Style::default()
                    .fg(theme.diagnostic_error_fg)
                    .bg(theme.popup_bg),
            ));
            let paragraph = Paragraph::new(vec![error_line]);
            frame.render_widget(paragraph, area);
            return visible_rows;
        }

        // Empty state
        if state.entries.is_empty() {
            let empty_line = Line::from(Span::styled(
                format!(" {}", t!("file_browser.empty")),
                Style::default()
                    .fg(theme.help_separator_fg)
                    .bg(theme.popup_bg),
            ));
            let paragraph = Paragraph::new(vec![empty_line]);
            frame.render_widget(paragraph, area);
            return visible_rows;
        }

        let mut lines = Vec::new();
        let visible_entries = state.visible_entries(visible_rows);

        for (view_idx, entry) in visible_entries.iter().enumerate() {
            let actual_idx = state.scroll_offset + view_idx;
            let is_selected = is_files_active && state.selected_index == Some(actual_idx);
            let is_hovered =
                matches!(hover_target, Some(HoverTarget::FileBrowserEntry(i)) if *i == actual_idx);

            // Base style based on selection, hover, and filter match
            let base_style = if is_selected {
                Style::default()
                    .fg(theme.popup_text_fg)
                    .bg(theme.suggestion_selected_bg)
            } else if is_hovered && entry.matches_filter {
                Style::default()
                    .fg(theme.menu_hover_fg)
                    .bg(theme.menu_hover_bg)
            } else if !entry.matches_filter {
                // Non-matching items are dimmed using the separator color
                Style::default()
                    .fg(theme.help_separator_fg)
                    .bg(theme.popup_bg)
                    .add_modifier(Modifier::DIM)
            } else {
                Style::default().fg(theme.popup_text_fg).bg(theme.popup_bg)
            };

            let mut spans = Vec::new();

            // Name column with trailing type indicator (dirs get /, symlinks get @)
            let name_with_indicator = if entry.fs_entry.is_dir() {
                format!("{}/", entry.fs_entry.name)
            } else if entry.fs_entry.is_symlink() {
                format!("{}@", entry.fs_entry.name)
            } else {
                entry.fs_entry.name.clone()
            };
            let name_display = if name_with_indicator.len() < name_col_width {
                format!("{:<width$}", name_with_indicator, width = name_col_width)
            } else {
                // Truncate with ellipsis
                let truncated: String = name_with_indicator
                    .chars()
                    .take(name_col_width - 3)
                    .collect();
                format!("{}...", truncated)
            };

            // Color directories differently
            let name_style = if entry.fs_entry.is_dir() && !is_selected {
                base_style.fg(theme.help_key_fg)
            } else {
                base_style
            };
            spans.push(Span::styled(name_display, name_style));

            // Size column
            let size_display = if entry.fs_entry.is_dir() {
                format!("{:>width$}", "--", width = size_col_width)
            } else {
                let size = entry
                    .fs_entry
                    .metadata
                    .as_ref()
                    .map(|m| format_size(m.size))
                    .unwrap_or_else(|| "--".to_string());
                format!("{:>width$}", size, width = size_col_width)
            };
            spans.push(Span::styled(size_display, base_style));

            // Separator
            spans.push(Span::styled("  ", base_style));

            // Modified column
            let modified_display = entry
                .fs_entry
                .metadata
                .as_ref()
                .and_then(|m| m.modified)
                .map(format_modified)
                .unwrap_or_else(|| "--".to_string());
            let modified_formatted =
                format!("{:>width$}", modified_display, width = date_col_width);
            spans.push(Span::styled(modified_formatted, base_style));

            lines.push(Line::from(spans));
        }

        // Fill remaining rows with empty lines
        while lines.len() < visible_rows {
            lines.push(Line::from(Span::styled(
                " ".repeat(width),
                Style::default().bg(theme.popup_bg),
            )));
        }

        let paragraph = Paragraph::new(lines);
        frame.render_widget(paragraph, area);

        visible_rows
    }
}

/// Layout information for mouse hit testing
#[derive(Debug, Clone)]
pub struct FileBrowserLayout {
    /// The overall popup area (including borders)
    pub popup_area: Rect,
    /// Navigation shortcuts area
    pub nav_area: Rect,
    /// Column headers area
    pub header_area: Rect,
    /// File list area
    pub list_area: Rect,
    /// Scrollbar area
    pub scrollbar_area: Rect,
    /// Scrollbar thumb start position
    pub thumb_start: usize,
    /// Scrollbar thumb end position
    pub thumb_end: usize,
    /// Number of visible rows in the file list
    pub visible_rows: usize,
    /// Width of the content area (for checkbox position calculation)
    pub content_width: u16,
}

impl FileBrowserLayout {
    /// Check if a position is within the overall popup area (including borders)
    pub fn contains(&self, x: u16, y: u16) -> bool {
        x >= self.popup_area.x
            && x < self.popup_area.x + self.popup_area.width
            && y >= self.popup_area.y
            && y < self.popup_area.y + self.popup_area.height
    }

    /// Check if a position is within the file list area
    pub fn is_in_list(&self, x: u16, y: u16) -> bool {
        x >= self.list_area.x
            && x < self.list_area.x + self.list_area.width
            && y >= self.list_area.y
            && y < self.list_area.y + self.list_area.height
    }

    /// Convert a click in the list area to an entry index
    pub fn click_to_index(&self, y: u16, scroll_offset: usize) -> Option<usize> {
        if y < self.list_area.y || y >= self.list_area.y + self.list_area.height {
            return None;
        }
        let row = (y - self.list_area.y) as usize;
        Some(scroll_offset + row)
    }

    /// Check if a position is in the navigation area
    pub fn is_in_nav(&self, x: u16, y: u16) -> bool {
        x >= self.nav_area.x
            && x < self.nav_area.x + self.nav_area.width
            && y >= self.nav_area.y
            && y < self.nav_area.y + self.nav_area.height
    }

    /// Determine which navigation shortcut was clicked based on x position
    /// The layout is: " Navigation: " (13 chars) then for each shortcut: " {label} " + " │ " separator
    /// Navigation shortcuts are on the second row (y == nav_area.y + 1)
    pub fn nav_shortcut_at(&self, x: u16, y: u16, shortcut_labels: &[&str]) -> Option<usize> {
        // Navigation shortcuts are on the second row of the nav area
        if y != self.nav_area.y + 1 {
            return None;
        }

        let rel_x = x.saturating_sub(self.nav_area.x) as usize;

        // Skip " Navigation: " prefix
        let prefix_len = 13;
        if rel_x < prefix_len {
            return None;
        }

        let mut current_x = prefix_len;
        for (idx, label) in shortcut_labels.iter().enumerate() {
            // Each shortcut: " {label} " = visual width + 2 spaces
            let shortcut_width = str_width(label) + 2;

            if rel_x >= current_x && rel_x < current_x + shortcut_width {
                return Some(idx);
            }
            current_x += shortcut_width;

            // Separator: " │ " = 3 chars
            if idx < shortcut_labels.len() - 1 {
                current_x += 3;
            }
        }

        None
    }

    /// Check if a position is in the header area (for sorting)
    pub fn is_in_header(&self, x: u16, y: u16) -> bool {
        x >= self.header_area.x
            && x < self.header_area.x + self.header_area.width
            && y >= self.header_area.y
            && y < self.header_area.y + self.header_area.height
    }

    /// Determine which column header was clicked
    pub fn header_column_at(&self, x: u16) -> Option<SortMode> {
        let rel_x = x.saturating_sub(self.header_area.x) as usize;
        let width = self.header_area.width as usize;

        let size_col_width = 10;
        let date_col_width = 14;
        let name_col_width = width.saturating_sub(size_col_width + date_col_width + 4);

        if rel_x < name_col_width {
            Some(SortMode::Name)
        } else if rel_x < name_col_width + size_col_width {
            Some(SortMode::Size)
        } else {
            Some(SortMode::Modified)
        }
    }

    /// Check if a position is in the scrollbar area
    pub fn is_in_scrollbar(&self, x: u16, y: u16) -> bool {
        x >= self.scrollbar_area.x
            && x < self.scrollbar_area.x + self.scrollbar_area.width
            && y >= self.scrollbar_area.y
            && y < self.scrollbar_area.y + self.scrollbar_area.height
    }

    /// Check if a position is in the scrollbar thumb
    pub fn is_in_thumb(&self, y: u16) -> bool {
        let rel_y = y.saturating_sub(self.scrollbar_area.y) as usize;
        rel_y >= self.thumb_start && rel_y < self.thumb_end
    }

    /// Check if a position is on the "Show Hidden" checkbox
    /// The checkbox is on the first row of navigation area
    /// Format: " ☐ Show Hidden (Alt+.) │ ☐ Detect Encoding (Alt+E)"
    pub fn is_on_show_hidden_checkbox(&self, x: u16, y: u16) -> bool {
        // Must be on the first row of navigation area (checkbox row)
        if y != self.nav_area.y {
            return false;
        }

        // Must be within the x bounds of the navigation area
        if x < self.nav_area.x || x >= self.nav_area.x + self.nav_area.width {
            return false;
        }

        // Show Hidden checkbox spans the left portion of the row
        // " ☐ Show Hidden (Alt+.)" is approximately 24 characters
        let show_hidden_width = 24u16;
        x < self.nav_area.x + show_hidden_width
    }

    /// Check if a position is on the "Detect Encoding" checkbox
    /// The checkbox is on the first row of navigation area, after Show Hidden
    /// Format: " ☐ Show Hidden (Alt+.) │ ☐ Detect Encoding (Alt+E)"
    pub fn is_on_detect_encoding_checkbox(&self, x: u16, y: u16) -> bool {
        // Must be on the first row of navigation area (checkbox row)
        if y != self.nav_area.y {
            return false;
        }

        // Must be within the x bounds of the navigation area
        if x < self.nav_area.x || x >= self.nav_area.x + self.nav_area.width {
            return false;
        }

        // Show Hidden + separator takes about 27 characters
        // " ☐ Show Hidden (Alt+.)" (24) + " │ " (3) = 27
        let detect_encoding_start = self.nav_area.x + 27;
        // Detect Encoding checkbox is about 28 characters
        // "☐ Detect Encoding (Alt+E)" (25+)
        let detect_encoding_end = detect_encoding_start + 28;

        x >= detect_encoding_start && x < detect_encoding_end
    }
}
