// src/modes/general/dialog.rs

use crossterm::event::{Event, KeyCode};
use crate::config::binds::config::Config;
use crate::ui::handlers::context::DialogPurpose;
use crate::state::app::{state::AppState, buffer::AppView};
use crate::state::app::buffer::BufferState;
use crate::state::pages::auth::{LoginState, RegisterState};
use crate::state::pages::admin::AdminState;
use crate::modes::handlers::event::EventOutcome;
use crate::tui::functions::common::{login, register};
use crate::tui::functions::common::add_table::handle_delete_selected_columns;
use anyhow::Result;

/// Handles key events specifically when a dialog is active.
/// Returns Some(Result<EventOutcome, Error>) if the event was handled (consumed),
/// otherwise returns None.
pub async fn handle_dialog_event(
    event: &Event,
    config: &Config,
    app_state: &mut AppState,
    login_state: &mut LoginState,
    register_state: &mut RegisterState,
    buffer_state: &mut BufferState,
    admin_state: &mut AdminState,
) -> Option<Result<EventOutcome>> {
    if let Event::Key(key) = event {
        // Always allow Esc to dismiss
        if key.code == KeyCode::Esc {
            app_state.hide_dialog();
            return Some(Ok(EventOutcome::Ok("Dialog dismissed".to_string())));
        }

        // Check general bindings for dialog actions
        if let Some(action) = config.get_general_action(key.code, key.modifiers) {
            match action {
                "move_down" | "next_option" => {
                    let current_index = app_state.ui.dialog.dialog_active_button_index;
                    let num_buttons = app_state.ui.dialog.dialog_buttons.len();
                    if num_buttons > 0 && current_index < num_buttons - 1 {
                        app_state.ui.dialog.dialog_active_button_index += 1;
                    }
                    return Some(Ok(EventOutcome::Ok(String::new())));
                }
                "move_up" | "previous_option" => {
                    let current_index = app_state.ui.dialog.dialog_active_button_index;
                    if current_index > 0 {
                        app_state.ui.dialog.dialog_active_button_index -= 1;
                    }
                    return Some(Ok(EventOutcome::Ok(String::new())));
                }
                "select" => {
                    let selected_index = app_state.ui.dialog.dialog_active_button_index;
                    let purpose = match app_state.ui.dialog.purpose {
                        Some(p) => p,
                        None => {
                            app_state.hide_dialog();
                            return Some(Ok(EventOutcome::Ok("Internal Error: Dialog context lost".to_string())));
                        }
                    };

                    // Handle Dialog Actions Directly Here
                    match purpose {
                        DialogPurpose::LoginSuccess => {
                            match selected_index {
                                0 => { // "Menu" button selected
                                    app_state.hide_dialog();
                                    let message = login::back_to_main(login_state, app_state, buffer_state).await;
                                    return Some(Ok(EventOutcome::Ok(message)));
                                }
                                1 => {
                                    app_state.hide_dialog();
                                    return Some(Ok(EventOutcome::Ok("Exiting dialog".to_string())));
                                }
                                _ => {
                                    app_state.hide_dialog();
                                    return Some(Ok(EventOutcome::Ok("Unknown dialog button selected".to_string())));
                                }
                            }
                        }
                        DialogPurpose::LoginFailed => {
                            match selected_index {
                                0 => { // "OK" button selected
                                    app_state.hide_dialog();
                                    return Some(Ok(EventOutcome::Ok("Login failed dialog dismissed".to_string())));
                                }
                                _ => {
                                    app_state.hide_dialog();
                                    return Some(Ok(EventOutcome::Ok("Unknown dialog button selected".to_string())));
                                }
                            }
                        }
                        DialogPurpose::RegisterSuccess => { // Add this arm
                            match selected_index {
                                0 => { // "OK" button for RegisterSuccess
                                    app_state.hide_dialog();
                                    let message = register::back_to_login(register_state, app_state, buffer_state).await;
                                    return Some(Ok(EventOutcome::Ok(message)));
                                }
                                _ => { // Default for RegisterSuccess
                                    app_state.hide_dialog();
                                    return Some(Ok(EventOutcome::Ok("Unknown dialog button selected".to_string())));
                                }
                            }
                        }
                        DialogPurpose::RegisterFailed => { // Add this arm
                             match selected_index {
                                0 => { // "OK" button for RegisterFailed
                                    app_state.hide_dialog(); // Just dismiss
                                    return Some(Ok(EventOutcome::Ok("Register failed dialog dismissed".to_string())));
                                }
                                _ => { // Default for RegisterFailed
                                    app_state.hide_dialog();
                                    return Some(Ok(EventOutcome::Ok("Unknown dialog button selected".to_string())));
                                }
                            }
                        }
                        DialogPurpose::ConfirmDeleteColumns => {
                            match selected_index {
                                0 => { // "Confirm" button selected
                                    let outcome_message = handle_delete_selected_columns(&mut admin_state.add_table_state);
                                    app_state.hide_dialog();
                                    return Some(Ok(EventOutcome::Ok(outcome_message)));
                                }
                                1 => { // "Cancel" button selected
                                    app_state.hide_dialog();
                                    return Some(Ok(EventOutcome::Ok("Deletion cancelled.".to_string())));
                                }
                                _ => { /* Handle unexpected index */ }
                            }
                        }
                        DialogPurpose::SaveTableSuccess => {
                            match selected_index {
                                0 => { // "OK" button selected
                                    app_state.hide_dialog();
                                    buffer_state.update_history(AppView::Admin); // Navigate back
                                    return Some(Ok(EventOutcome::Ok("Save success dialog dismissed.".to_string())));
                                }
                                _ => { /* Handle unexpected index */ }
                            }
                        }
                        DialogPurpose::SaveLogicSuccess => {
                            match selected_index {
                                0 => { // "OK" button selected
                                    app_state.hide_dialog();
                                    buffer_state.update_history(AppView::Admin);
                                    return Some(Ok(EventOutcome::Ok("Save success dialog dismissed.".to_string())));
                                }
                                _ => { /* Handle unexpected index */ }
                            }
                        }
                    }
                }
                _ => {} // Ignore other general actions when dialog is shown
            }
        }
        // If it was a key event but not handled above, consume it
        Some(Ok(EventOutcome::Ok(String::new())))
    } else {
        // If it wasn't a key event, consume it too while dialog is active
        Some(Ok(EventOutcome::Ok(String::new())))
    }
}
