use crate::tui::app::*;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

pub fn handle_normal_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Char('C') => app.focused_panel = FocusedPanel::Collections,
        KeyCode::Char('R') => {
            app.focus_request_bar();
            app.cursor_position = app.url.len();
        }
        KeyCode::Char('P') => {
            if app.current_request_id.is_some() {
                app.selected_property_tab = PropertyTab::Params;
                app.focused_panel = FocusedPanel::Details;
            }
        }
        KeyCode::Char('H') => {
            if app.current_request_id.is_some() {
                app.selected_property_tab = PropertyTab::Headers;
                app.focused_panel = FocusedPanel::Details;
            }
        }
        KeyCode::Char('U') => {
            if app.current_request_id.is_some() {
                app.selected_property_tab = PropertyTab::Auth;
                app.focused_panel = FocusedPanel::Details;
            }
        }
        KeyCode::Char('B') => {
            if app.current_request_id.is_some() {
                app.selected_property_tab = PropertyTab::Body;
                app.focused_panel = FocusedPanel::Details;
            }
        }
        KeyCode::Char('S') => {
            if app.current_request_id.is_some() {
                app.selected_property_tab = PropertyTab::Scripts;
                app.focused_panel = FocusedPanel::Details;
            }
        }
        KeyCode::Char('E') => app.focused_panel = FocusedPanel::Response,
        KeyCode::Char('T') => app.focused_panel = FocusedPanel::Stats,
        KeyCode::Char('V') => {
            app.left_bottom_tab = crate::tui::app::LeftBottomTab::Environments;
            app.focused_panel = FocusedPanel::Environments;
        }
        KeyCode::Char('A') => {
            app.left_bottom_tab = crate::tui::app::LeftBottomTab::Apis;
            app.focused_panel = FocusedPanel::Apis;
        }
        KeyCode::Char('m') => {
            if app.focused_panel == FocusedPanel::Environments {
                app.toggle_env_mask();
            }
        }

        // gg and G shortcuts
        KeyCode::Char('g') => {
            if app.g_pressed {
                match app.focused_panel {
                    FocusedPanel::Collections => app.selected_collection_index = 0,
                    FocusedPanel::Apis => app.selected_api_index = 0,
                    FocusedPanel::Environments => app.selected_env_index = 0,
                    FocusedPanel::Details => app.property_editor_row = 0,
                    FocusedPanel::Response => app.response_scroll = 0,
                    FocusedPanel::Stats => app.stats_scroll = 0,
                    _ => {}
                }
                app.g_pressed = false;
            } else {
                app.g_pressed = true;
            }
            return;
        }
        KeyCode::Char('G') => match app.focused_panel {
            FocusedPanel::Collections => {
                app.selected_collection_index =
                    app.get_visible_collections().len().saturating_sub(1)
            }
            FocusedPanel::Apis => {
                app.selected_api_index = app.get_visible_items().len().saturating_sub(1)
            }
            FocusedPanel::Environments => {
                app.selected_env_index =
                    app.get_active_collection_env_vars().len().saturating_sub(1)
            }
            FocusedPanel::Details => {
                if let Some(req) = app.get_current_request() {
                    let max_rows = match app.selected_property_tab {
                        PropertyTab::Params => req.params.len(),
                        PropertyTab::Headers => req.headers.len(),
                        PropertyTab::Auth => match req.auth.selected {
                            crate::core::collection::AuthType::None => 0,
                            crate::core::collection::AuthType::Bearer => 1,
                            crate::core::collection::AuthType::Basic => 2,
                            crate::core::collection::AuthType::ApiKey => 3,
                        },
                        PropertyTab::Body => match req.body.selected {
                            crate::core::collection::BodyType::FormData => {
                                req.body.form_data.items.len()
                            }
                            crate::core::collection::BodyType::XWwwFormUrlEncoded => {
                                req.body.x_www_form_urlencoded.items.len()
                            }
                            _ => 0,
                        },
                        _ => 0,
                    };
                    app.property_editor_row = max_rows.saturating_sub(1);
                }
            }
            FocusedPanel::Response => {
                app.response_scroll = 1000;
            }
            FocusedPanel::Stats => {
                app.stats_scroll = 100;
            }
            _ => {}
        },

        // Page Up/Down (Ctrl-u / Ctrl-d)
        KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            match app.focused_panel {
                FocusedPanel::Collections => {
                    app.selected_collection_index =
                        app.selected_collection_index.saturating_sub(10);
                }
                FocusedPanel::Apis => {
                    app.selected_api_index = app.selected_api_index.saturating_sub(10);
                }
                FocusedPanel::Details => {
                    app.property_editor_row = app.property_editor_row.saturating_sub(10);
                    app.details_scroll = app.details_scroll.saturating_sub(10);
                }
                FocusedPanel::Response => {
                    app.response_scroll = app.response_scroll.saturating_sub(10);
                }
                FocusedPanel::Stats => {
                    app.stats_scroll = app.stats_scroll.saturating_sub(10);
                }
                _ => {}
            }
        }
        KeyCode::Char('d') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            match app.focused_panel {
                FocusedPanel::Collections => {
                    let max = app.get_visible_collections().len().saturating_sub(1);
                    app.selected_collection_index = (app.selected_collection_index + 10).min(max);
                }
                FocusedPanel::Apis => {
                    let max = app.get_visible_items().len().saturating_sub(1);
                    app.selected_api_index = (app.selected_api_index + 10).min(max);
                }
                FocusedPanel::Details => {
                    app.property_editor_row += 10; // Capped in render/logic later
                    app.details_scroll = app.details_scroll.saturating_add(10);
                }
                FocusedPanel::Response => {
                    app.response_scroll = app.response_scroll.saturating_add(10);
                }
                FocusedPanel::Stats => {
                    app.stats_scroll = app.stats_scroll.saturating_add(10);
                }
                _ => {}
            }
        }

        KeyCode::Char('q') => app.input_mode = InputMode::ConfirmQuit,
        KeyCode::Char('?') => app.input_mode = InputMode::Help,
        KeyCode::Tab => app.next_panel(),
        KeyCode::BackTab => app.prev_panel(),

        // Navigation Down
        KeyCode::Char('j') | KeyCode::Down => match app.focused_panel {
            FocusedPanel::Collections => {
                let max_idx = app.get_visible_collections().len().saturating_sub(1);
                if app.selected_collection_index < max_idx {
                    app.selected_collection_index += 1;
                    app.update_active_scope_from_tree();
                }
            }
            FocusedPanel::Apis => {
                let visible_items = app.get_visible_items();
                if app.selected_api_index < visible_items.len().saturating_sub(1) {
                    app.selected_api_index += 1;
                }
            }
            FocusedPanel::Environments => {
                let env_vars = app.get_active_collection_env_vars();
                if app.selected_env_index < env_vars.len().saturating_sub(1) {
                    app.selected_env_index += 1;
                }
            }
            FocusedPanel::Details => {
                if let Some(req) = app.get_current_request() {
                    let is_kv_tab = matches!(
                        app.selected_property_tab,
                        PropertyTab::Params | PropertyTab::Headers | PropertyTab::Auth
                    ) || (app.selected_property_tab == PropertyTab::Body
                        && matches!(
                            req.body.selected,
                            crate::core::collection::BodyType::FormData
                                | crate::core::collection::BodyType::XWwwFormUrlEncoded
                        ));

                    if is_kv_tab {
                        let max_rows = match app.selected_property_tab {
                            PropertyTab::Params => req.params.len(),
                            PropertyTab::Headers => req.headers.len(),
                            PropertyTab::Auth => match req.auth.selected {
                                crate::core::collection::AuthType::None => 0,
                                crate::core::collection::AuthType::Bearer => 1,
                                crate::core::collection::AuthType::Basic => 2,
                                crate::core::collection::AuthType::ApiKey => 3,
                            },
                            PropertyTab::Body => match req.body.selected {
                                crate::core::collection::BodyType::FormData => {
                                    req.body.form_data.items.len()
                                }
                                crate::core::collection::BodyType::XWwwFormUrlEncoded => {
                                    req.body.x_www_form_urlencoded.items.len()
                                }
                                _ => 0,
                            },
                            _ => 0,
                        };
                        if app.property_editor_row < max_rows.saturating_sub(1) {
                            app.property_editor_row += 1;
                        }
                    } else {
                        app.details_scroll = app.details_scroll.saturating_add(1);
                    }
                }
            }
            FocusedPanel::Response => {
                app.response_cursor_row = app.response_cursor_row.saturating_add(1);
            }
            FocusedPanel::Stats => {
                app.stats_scroll = app.stats_scroll.saturating_add(1);
            }
            _ => {}
        },

        // Navigation Up
        KeyCode::Char('k') | KeyCode::Up => match app.focused_panel {
            FocusedPanel::Collections => {
                if app.selected_collection_index > 0 {
                    app.selected_collection_index -= 1;
                    app.update_active_scope_from_tree();
                }
            }
            FocusedPanel::Apis => {
                if app.selected_api_index > 0 {
                    app.selected_api_index -= 1;
                }
            }
            FocusedPanel::Environments => {
                if app.selected_env_index > 0 {
                    app.selected_env_index -= 1;
                }
            }
            FocusedPanel::Details => {
                if let Some(req) = app.get_current_request() {
                    let is_kv_tab = matches!(
                        app.selected_property_tab,
                        PropertyTab::Params | PropertyTab::Headers | PropertyTab::Auth
                    ) || (app.selected_property_tab == PropertyTab::Body
                        && matches!(
                            req.body.selected,
                            crate::core::collection::BodyType::FormData
                                | crate::core::collection::BodyType::XWwwFormUrlEncoded
                        ));

                    if is_kv_tab {
                        if app.property_editor_row > 0 {
                            app.property_editor_row -= 1;
                        }
                    } else {
                        app.details_scroll = app.details_scroll.saturating_sub(1);
                    }
                }
            }
            FocusedPanel::Response => {
                app.response_cursor_row = app.response_cursor_row.saturating_sub(1);
            }
            FocusedPanel::Stats => {
                app.stats_scroll = app.stats_scroll.saturating_sub(1);
            }
            _ => {}
        },

        // Drill down / Enter
        KeyCode::Enter => match app.focused_panel {
            FocusedPanel::Collections => {
                let visible_collections = app.get_visible_collections();
                if let Some(item) = visible_collections.get(app.selected_collection_index) {
                    match &item.item_type {
                        crate::tui::app::VisibleItemType::Collection { .. }
                        | crate::tui::app::VisibleItemType::Folder { .. } => {
                            app.toggle_folder();
                        }
                        crate::tui::app::VisibleItemType::Request { method, id, .. } => {
                            app.save_current_request();
                            app.current_request_id = Some(id.clone());
                            app.method = *method;
                            let id_clone = id.clone();
                            for col in &mut app.collections {
                                if let Some(req) = col.find_request_mut(&id_clone) {
                                    app.url = req.url.clone();
                                    req.auth.auto_select();
                                    req.body.auto_select();
                                    break;
                                }
                            }
                            app.focus_request_bar();
                            app.cursor_position = app.url.len();
                            app.reset_scroll();
                        }
                    }
                }
            }
            FocusedPanel::Apis => {
                let visible_items = app.get_visible_items();
                if let Some(item) = visible_items.get(app.selected_api_index) {
                    match &item.item_type {
                        crate::tui::app::VisibleItemType::Folder { .. } => {
                            app.toggle_folder();
                        }
                        crate::tui::app::VisibleItemType::Request { method, id, .. } => {
                            app.save_current_request();
                            app.current_request_id = Some(id.clone());
                            app.method = *method;
                            let id_clone = id.clone();
                            if let Some(col) = app.collections.get_mut(app.active_collection_index)
                            {
                                if let Some(req) = col.find_request_mut(&id_clone) {
                                    app.url = req.url.clone();
                                    req.auth.auto_select();
                                    req.body.auto_select();
                                }
                            }
                            app.focus_request_bar();
                            app.cursor_position = app.url.len();
                            app.reset_scroll();
                        }
                        _ => {}
                    }
                }
            }
            FocusedPanel::Environments => {
                app.input_mode = InputMode::Editing;
                app.property_editor_field = PropertyEditorField::Value;
                let current_val = app.get_env_editor_value();
                app.cursor_position = current_val.len();
            }
            FocusedPanel::Properties => {
                app.focused_panel = FocusedPanel::Details;
                // For Auth, always start on the field name (Key)
                if app.selected_property_tab == PropertyTab::Auth {
                    app.property_editor_field = PropertyEditorField::Key;
                }
            }
            FocusedPanel::Details => {
                if let Some(req) = app.get_current_request() {
                    if app.selected_property_tab == PropertyTab::Auth {
                        if req.auth.selected == crate::core::collection::AuthType::None {
                            return;
                        }
                        if req.auth.selected == crate::core::collection::AuthType::ApiKey {
                            if app.property_editor_row == 2 {
                                app.toggle_auth_bool();
                                return;
                            }
                        }
                        // Always switch to Value field when starting to edit Auth
                        app.property_editor_field = PropertyEditorField::Value;
                    } else if matches!(
                        app.selected_property_tab,
                        PropertyTab::Params | PropertyTab::Headers
                    ) {
                        let items = match app.selected_property_tab {
                            PropertyTab::Params => &req.params,
                            PropertyTab::Headers => &req.headers,
                            _ => unreachable!(),
                        };
                        if items.is_empty() {
                            return;
                        }
                    } else if app.selected_property_tab == PropertyTab::Body {
                        match req.body.selected {
                            crate::core::collection::BodyType::FormData => {
                                if req.body.form_data.items.is_empty() {
                                    return;
                                }
                            }
                            crate::core::collection::BodyType::XWwwFormUrlEncoded => {
                                if req.body.x_www_form_urlencoded.items.is_empty() {
                                    return;
                                }
                            }
                            crate::core::collection::BodyType::Raw
                            | crate::core::collection::BodyType::None => {
                                return;
                            }
                        }
                    } else {
                        // Scripts or other tabs
                        return;
                    }

                    app.input_mode = InputMode::Editing;
                    let current_val = app.get_kv_editor_value();
                    app.cursor_position = current_val.len();
                }
            }
            FocusedPanel::RequestBar => match app.active_request_part {
                RequestBarPart::Method => {
                    app.show_method_search = true;
                    app.method_search_query.clear();
                    app.cursor_position = 0;
                }
                RequestBarPart::Url => {
                    app.input_mode = InputMode::Editing;
                    app.cursor_position = app.url.len();
                }
                RequestBarPart::Send => {
                    app.pending_actions.push(TuiAction::SendRequest);
                    app.focused_panel = FocusedPanel::Response;
                }
            },
            _ => {}
        },

        // Move Right / Next Tab
        KeyCode::Char('l') | KeyCode::Right => match app.focused_panel {
            FocusedPanel::Properties => {
                app.next_property_tab();
            }
            FocusedPanel::Details => match app.selected_property_tab {
                PropertyTab::Params | PropertyTab::Headers => {
                    app.property_editor_field = match app.property_editor_field {
                        PropertyEditorField::Key => PropertyEditorField::Value,
                        PropertyEditorField::Value => PropertyEditorField::Description,
                        PropertyEditorField::Description => PropertyEditorField::Description,
                    };
                }
                _ => {}
            },
            FocusedPanel::Environments => {
                app.property_editor_field = match app.property_editor_field {
                    PropertyEditorField::Key => PropertyEditorField::Value,
                    PropertyEditorField::Value => PropertyEditorField::Value,
                    _ => PropertyEditorField::Value,
                };
            }
            FocusedPanel::RequestBar => {
                app.active_request_part = match app.active_request_part {
                    RequestBarPart::Method => RequestBarPart::Url,
                    RequestBarPart::Url => RequestBarPart::Send,
                    RequestBarPart::Send => RequestBarPart::Method,
                };
            }
            FocusedPanel::Response => {
                app.response_cursor_col = app.response_cursor_col.saturating_add(1);
            }
            FocusedPanel::Stats => {
                app.cycle_stats_tab();
            }
            _ => {}
        },

        // Move Left / Prev Tab
        KeyCode::Char('h') | KeyCode::Left => match app.focused_panel {
            FocusedPanel::Properties => {
                app.prev_property_tab();
            }
            FocusedPanel::Details => match app.selected_property_tab {
                PropertyTab::Params | PropertyTab::Headers => {
                    app.property_editor_field = match app.property_editor_field {
                        PropertyEditorField::Key => PropertyEditorField::Key,
                        PropertyEditorField::Value => PropertyEditorField::Key,
                        PropertyEditorField::Description => PropertyEditorField::Value,
                    };
                }
                _ => {}
            },
            FocusedPanel::Environments => {
                app.property_editor_field = match app.property_editor_field {
                    PropertyEditorField::Key => PropertyEditorField::Key,
                    PropertyEditorField::Value => PropertyEditorField::Key,
                    _ => PropertyEditorField::Key,
                };
            }
            FocusedPanel::RequestBar => {
                app.active_request_part = match app.active_request_part {
                    RequestBarPart::Method => RequestBarPart::Send,
                    RequestBarPart::Url => RequestBarPart::Method,
                    RequestBarPart::Send => RequestBarPart::Url,
                };
            }
            FocusedPanel::Response => {
                app.response_cursor_col = app.response_cursor_col.saturating_sub(1);
            }
            FocusedPanel::Stats => {
                app.prev_stats_tab();
            }
            _ => {
                app.pop_up();
            }
        },

        // Pop up explicitly
        KeyCode::Esc => {
            app.pop_up();
        }

        KeyCode::Char(' ') => {
            if app.focused_panel == FocusedPanel::Apis
                || app.focused_panel == FocusedPanel::Collections
            {
                app.toggle_folder();
            } else if app.focused_panel == FocusedPanel::Details {
                app.toggle_kv_param();
            }
        }

        KeyCode::Char('/') => {
            if app.focused_panel == FocusedPanel::Apis
                || app.focused_panel == FocusedPanel::Collections
            {
                app.input_mode = InputMode::Search;
                app.show_search = true;
                app.search_query.clear();
                app.cursor_position = 0;
            }
        }

        KeyCode::Char('e') => {
            app.focus_request_bar();
            app.cursor_position = app.url.len();
        }

        KeyCode::Char('a') => {
            if app.focused_panel == FocusedPanel::Apis
                || app.focused_panel == FocusedPanel::Collections
            {
                app.input_mode = InputMode::CreateItem;
                app.pending_item_type = Some(PendingItemType::Request);
                app.rename_input.clear();
                app.cursor_position = 0;
                return;
            } else if app.focused_panel == FocusedPanel::Details {
                if let Some(req) = app.get_current_request() {
                    // Check if we can add based on the current tab and its state
                    let can_add = match app.selected_property_tab {
                        PropertyTab::Auth => {
                            // Don't allow adding in Auth tab if it's set to None
                            req.auth.selected != crate::core::collection::AuthType::None
                        }
                        PropertyTab::Params | PropertyTab::Headers => true,
                        PropertyTab::Body => {
                            // Only allow for FormData and XWwwFormUrlEncoded, not Raw or None
                            match req.body.selected {
                                crate::core::collection::BodyType::FormData
                                | crate::core::collection::BodyType::XWwwFormUrlEncoded => true,
                                _ => false,
                            }
                        }
                        _ => false,
                    };

                    if can_add {
                        app.add_kv_param(String::new());
                        app.input_mode = InputMode::Editing;
                        app.property_editor_field = PropertyEditorField::Key;
                        app.cursor_position = 0;
                    }
                    // Always return - don't process 'a' further if in Details panel
                    return;
                }
            } else if app.focused_panel == FocusedPanel::Environments {
                app.add_env_var(String::new());
                app.input_mode = InputMode::Editing;
                app.property_editor_field = PropertyEditorField::Key;
                app.cursor_position = 0;
                return;
            }
        }

        KeyCode::Char('f') => {
            if app.focused_panel == FocusedPanel::Apis
                || app.focused_panel == FocusedPanel::Collections
            {
                app.input_mode = InputMode::CreateItem;
                app.pending_item_type = Some(PendingItemType::Folder);
                app.rename_input.clear();
                app.cursor_position = 0;
            }
        }

        KeyCode::Char('c') => {
            if app.focused_panel == FocusedPanel::Details
                && app.selected_property_tab == PropertyTab::Body
            {
                app.cycle_raw_content_type();
            } else if app.focused_panel == FocusedPanel::Response {
                app.pending_actions.push(TuiAction::CopyResponseValue);
            }
        }

        KeyCode::Char('t') => {
            if app.focused_panel == FocusedPanel::Details {
                match app.selected_property_tab {
                    PropertyTab::Auth => app.cycle_auth_type(),
                    PropertyTab::Body => app.cycle_body_type(),
                    _ => {}
                }
            } else if app.focused_panel == FocusedPanel::Stats {
                app.cycle_stats_tab();
            }
        }

        KeyCode::Char('n') => {
            if app.focused_panel == FocusedPanel::Collections {
                app.input_mode = InputMode::CreateItem;
                app.pending_item_type = Some(PendingItemType::Collection);
                app.rename_input.clear();
                app.cursor_position = 0;
            }
        }

        KeyCode::Char('d') => {
            if app.focused_panel == FocusedPanel::Details {
                app.delete_kv_param();
            } else if app.focused_panel == FocusedPanel::Environments {
                app.delete_env_var();
            } else {
                app.input_mode = InputMode::ConfirmDelete;
            }
        }

        KeyCode::Char('r') => {
            if app.focused_panel == FocusedPanel::Apis
                || app.focused_panel == FocusedPanel::Collections
            {
                app.input_mode = InputMode::Rename;
                app.rename_input.clear();
                if app.focused_panel == FocusedPanel::Collections {
                    let visible_collections = app.get_visible_collections();
                    if let Some(item) = visible_collections.get(app.selected_collection_index) {
                        app.rename_input = item.name.clone();
                    }
                } else if app.focused_panel == FocusedPanel::Apis {
                    let visible_items = app.get_visible_items();
                    if let Some(item) = visible_items.get(app.selected_api_index) {
                        app.rename_input = item.name.clone();
                    }
                }
                app.cursor_position = app.rename_input.len();
            } else if app.focused_panel == FocusedPanel::Environments {
                app.input_mode = InputMode::Editing;
                app.property_editor_field = PropertyEditorField::Key;
                let current_val = app.get_env_editor_value();
                app.cursor_position = current_val.len();
            }
        }

        KeyCode::Char('v') => {
            if app.focused_panel == FocusedPanel::Details
                && app.selected_property_tab == PropertyTab::Body
            {
                app.pending_actions.push(TuiAction::EditBody);
            }
        }

        KeyCode::Char('y') => {
            if app.focused_panel == FocusedPanel::Details
                && app.selected_property_tab == PropertyTab::Body
            {
                app.pending_actions.push(TuiAction::CopyBody);
            } else if app.focused_panel == FocusedPanel::Response {
                app.pending_actions.push(TuiAction::CopyResponseValue);
            } else if app.focused_panel == FocusedPanel::Details {
                app.pending_actions.push(TuiAction::Copy);
            } else if app.focused_panel == FocusedPanel::Environments {
                app.pending_actions.push(TuiAction::Copy);
            }
        }

        KeyCode::Char('p') => {
            if app.focused_panel == FocusedPanel::Details
                && app.selected_property_tab == PropertyTab::Body
            {
                app.pending_actions.push(TuiAction::PasteBody);
            } else if app.focused_panel == FocusedPanel::Details
                || app.focused_panel == FocusedPanel::Environments
            {
                app.pending_actions.push(TuiAction::Paste);
            }
        }

        KeyCode::Char('Y') => {
            if app.focused_panel == FocusedPanel::Response {
                app.pending_actions.push(TuiAction::CopyResponseAll);
            }
        }

        KeyCode::Char(':') => {
            app.input_mode = InputMode::Command;
            app.command_input.clear();
            app.cursor_position = 0;
        }

        KeyCode::Char('i') => {
            if app.focused_panel == FocusedPanel::Details {
                match app.selected_property_tab {
                    PropertyTab::Params | PropertyTab::Headers | PropertyTab::Auth => {
                        if let Some(req) = app.get_current_request() {
                            if app.selected_property_tab == PropertyTab::Auth {
                                if req.auth.selected == crate::core::collection::AuthType::None {
                                    return;
                                }
                                if req.auth.selected == crate::core::collection::AuthType::ApiKey {
                                    if app.property_editor_row == 2 {
                                        // It's a boolean, don't enter editing mode, just toggle
                                        app.toggle_auth_bool();
                                        return;
                                    }
                                }
                                app.property_editor_field = PropertyEditorField::Value;
                            } else {
                                // For Params and Headers, check if there's a row to edit
                                let items = match app.selected_property_tab {
                                    PropertyTab::Params => &req.params,
                                    PropertyTab::Headers => &req.headers,
                                    _ => &Vec::new(),
                                };
                                if items.is_empty() {
                                    return;
                                }
                            }

                            app.input_mode = InputMode::Editing;
                            let current_val = app.get_kv_editor_value();
                            app.cursor_position = current_val.len();
                        }
                    }
                    _ => {}
                }
            }
        }

        _ => {}
    }
}
