
// src/modes/handlers/event_helper.rs
//! Helper functions to handle the differences between legacy and library CanvasState traits

use crate::state::app::state::AppState;
use crate::state::pages::{
    form::FormState,
    auth::{LoginState, RegisterState},
};
use crate::state::pages::canvas_state::CanvasState as LegacyCanvasState;
use canvas::canvas::CanvasState as LibraryCanvasState;

/// Get the current field index from the appropriate state based on which UI is active
pub fn get_current_field_for_state(
    app_state: &AppState,
    login_state: &LoginState,
    register_state: &RegisterState,
    form_state: &FormState,
) -> usize {
    if app_state.ui.show_login {
        login_state.current_field() // Uses LegacyCanvasState
    } else if app_state.ui.show_register {
        register_state.current_field() // Uses LegacyCanvasState
    } else {
        form_state.current_field() // Uses LibraryCanvasState
    }
}

/// Get the current cursor position from the appropriate state based on which UI is active
pub fn get_current_cursor_pos_for_state(
    app_state: &AppState,
    login_state: &LoginState,
    register_state: &RegisterState,
    form_state: &FormState,
) -> usize {
    if app_state.ui.show_login {
        login_state.current_cursor_pos() // Uses LegacyCanvasState
    } else if app_state.ui.show_register {
        register_state.current_cursor_pos() // Uses LegacyCanvasState
    } else {
        form_state.current_cursor_pos() // Uses LibraryCanvasState
    }
}

/// Check if the appropriate state has unsaved changes based on which UI is active
pub fn get_has_unsaved_changes_for_state(
    app_state: &AppState,
    login_state: &LoginState,
    register_state: &RegisterState,
    form_state: &FormState,
) -> bool {
    if app_state.ui.show_login {
        login_state.has_unsaved_changes() // Uses LegacyCanvasState
    } else if app_state.ui.show_register {
        register_state.has_unsaved_changes() // Uses LegacyCanvasState
    } else {
        form_state.has_unsaved_changes() // Uses LibraryCanvasState
    }
}

/// Get the current input from the appropriate state based on which UI is active
pub fn get_current_input_for_state<'a>(
    app_state: &AppState,
    login_state: &'a LoginState,
    register_state: &'a RegisterState,
    form_state: &'a FormState,
) -> &'a str {
    if app_state.ui.show_login {
        login_state.get_current_input() // Uses LegacyCanvasState
    } else if app_state.ui.show_register {
        register_state.get_current_input() // Uses LegacyCanvasState
    } else {
        form_state.get_current_input() // Uses LibraryCanvasState
    }
}

/// Set the cursor position for the appropriate state based on which UI is active
pub fn set_current_cursor_pos_for_state(
    app_state: &AppState,
    login_state: &mut LoginState,
    register_state: &mut RegisterState,
    form_state: &mut FormState,
    pos: usize,
) {
    if app_state.ui.show_login {
        login_state.set_current_cursor_pos(pos); // Uses LegacyCanvasState
    } else if app_state.ui.show_register {
        register_state.set_current_cursor_pos(pos); // Uses LegacyCanvasState
    } else {
        form_state.set_current_cursor_pos(pos); // Uses LibraryCanvasState
    }
}

/// Get cursor position for mixed login/register vs form logic
pub fn get_cursor_pos_for_mixed_state(
    app_state: &AppState,
    login_state: &LoginState,
    form_state: &FormState,
) -> usize {
    if app_state.ui.show_login || app_state.ui.show_register {
        login_state.current_cursor_pos() // Uses LegacyCanvasState
    } else {
        form_state.current_cursor_pos() // Uses LibraryCanvasState
    }
}
