trackWork 0.15.0

A terminal-based time tracking application for managing work sessions
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use crate::app::{App, InputMode};
use crate::integrations::IntegrationKind;

/// Check if the current settings field is a color picker field
fn is_color_field(current_field: usize, integration: &IntegrationKind) -> bool {
    let colors_start = match integration {
        IntegrationKind::CustomCommands => 8,
        IntegrationKind::Jira => 9,
    };
    current_field >= colors_start && current_field < colors_start + 6
}

/// Check if the current settings field is the Hide Eye Candy toggle
fn is_hide_eye_candy_field(current_field: usize, integration: &IntegrationKind) -> bool {
    match integration {
        IntegrationKind::CustomCommands => current_field == 7,
        IntegrationKind::Jira => current_field == 8,
    }
}

/// Check if the current settings field is the passphrase button
fn is_passphrase_field(current_field: usize, integration: &IntegrationKind) -> bool {
    match integration {
        IntegrationKind::CustomCommands => current_field == 5,
        IntegrationKind::Jira => current_field == 6,
    }
}

/// Check if the current settings field is the Triggers button
fn is_triggers_field(current_field: usize, integration: &IntegrationKind) -> bool {
    match integration {
        IntegrationKind::CustomCommands => current_field == 6,
        IntegrationKind::Jira => current_field == 7,
    }
}

/// Handle keyboard input for Settings mode
pub fn handle_settings_input(app: &mut App, key: KeyEvent) {
    // Check current field and integration to determine special handling
    let (current_field, integration_kind) = if let InputMode::Settings {
        current_field,
        integration,
        ..
    } = &app.input_mode
    {
        (*current_field, integration.clone())
    } else {
        (0, IntegrationKind::CustomCommands)
    };

    let legacy_field = app.settings_legacy_time_format_field();
    let _passphrase_field = app.settings_passphrase_field();
    let on_color = is_color_field(current_field, &integration_kind);
    let on_passphrase = is_passphrase_field(current_field, &integration_kind);
    let on_triggers = is_triggers_field(current_field, &integration_kind);
    let on_hide_eye_candy = is_hide_eye_candy_field(current_field, &integration_kind);

    match key.code {
        KeyCode::Enter | KeyCode::Char(' ') if on_triggers => {
            app.open_triggers();
        }
        KeyCode::Char(_) | KeyCode::Backspace if on_triggers => {
            // Ignore typing on the Triggers button.
        }
        KeyCode::Enter if on_passphrase => {
            let is_initial = !app.secrets.has_encrypted_file();
            app.input_mode = InputMode::PassphraseChange {
                old_passphrase: String::new(),
                new_passphrase: String::new(),
                confirm_passphrase: String::new(),
                current_field: 0,
                cursor_pos: 0,
                error_message: None,
                is_initial_setup: is_initial,
            };
        }
        KeyCode::Char(' ') if on_passphrase => {
            let is_initial = !app.secrets.has_encrypted_file();
            app.input_mode = InputMode::PassphraseChange {
                old_passphrase: String::new(),
                new_passphrase: String::new(),
                confirm_passphrase: String::new(),
                current_field: 0,
                cursor_pos: 0,
                error_message: None,
                is_initial_setup: is_initial,
            };
        }
        KeyCode::Enter => {
            if let Err(_e) = app.save_settings() {
                // If there's an error saving, just ignore it and stay in settings mode
            }
        }
        KeyCode::Esc => {
            app.cancel_input();
        }
        KeyCode::Tab if key.modifiers.contains(KeyModifiers::SHIFT) => {
            app.previous_field()
        }
        KeyCode::Tab => app.next_field(),
        KeyCode::Up if key.modifiers.contains(KeyModifiers::SHIFT) => {
            app.scroll_debug_log(-1);
        }
        KeyCode::Down if key.modifiers.contains(KeyModifiers::SHIFT) => {
            app.scroll_debug_log(1);
        }
        KeyCode::Up if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.scroll_debug_log(-1);
        }
        KeyCode::Down if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.scroll_debug_log(1);
        }
        KeyCode::Up => app.previous_field(),
        KeyCode::Down => app.next_field(),
        // Field 0: Integration selector
        KeyCode::Left if current_field == 0 => {
            app.cycle_integration();
        }
        KeyCode::Right if current_field == 0 => {
            app.cycle_integration();
        }
        KeyCode::Char(' ') if current_field == 0 => {
            app.cycle_integration();
        }
        KeyCode::Char(_) if current_field == 0 => {
            // Ignore text input on integration selector
        }
        KeyCode::Backspace if current_field == 0 => {
            // Ignore backspace on integration selector
        }
        // Legacy time format toggle
        KeyCode::Left if current_field == legacy_field => {
            app.toggle_legacy_time_format();
        }
        KeyCode::Right if current_field == legacy_field => {
            app.toggle_legacy_time_format();
        }
        KeyCode::Char(' ') if current_field == legacy_field => {
            app.toggle_legacy_time_format();
        }
        KeyCode::Char(_) if current_field == legacy_field => {
            // Ignore text input on legacy time format field
        }
        KeyCode::Backspace if current_field == legacy_field => {
            // Ignore backspace on legacy time format field
        }
        // Hide Eye Candy toggle
        KeyCode::Left if on_hide_eye_candy => app.toggle_hide_eye_candy(),
        KeyCode::Right if on_hide_eye_candy => app.toggle_hide_eye_candy(),
        KeyCode::Char(' ') if on_hide_eye_candy => app.toggle_hide_eye_candy(),
        KeyCode::Char(_) if on_hide_eye_candy => {}
        KeyCode::Backspace if on_hide_eye_candy => {}
        // Passphrase button field
        KeyCode::Left if on_passphrase => {}
        KeyCode::Right if on_passphrase => {}
        KeyCode::Char(_) if on_passphrase => {}
        KeyCode::Backspace if on_passphrase => {}
        // Color cycling (left/right on color fields)
        KeyCode::Left if on_color => app.cycle_color(false),
        KeyCode::Right if on_color => app.cycle_color(true),
        // Text field cursor movement (left/right on text fields)
        KeyCode::Left => app.cursor_left(),
        KeyCode::Right => app.cursor_right(),
        KeyCode::Char('+') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.insert_variable();
        }
        KeyCode::Char(c) => app.input_char(c),
        KeyCode::Backspace => app.delete_char(),
        _ => {}
    }
}