// src/modes/common/command_mode.rs

use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
use crate::config::binds::config::Config;
use crate::services::grpc_client::GrpcClient;
use crate::state::app::state::AppState;
use crate::modes::common::commands::CommandHandler;
use crate::tui::terminal::core::TerminalCore;
use crate::pages::forms::logic::{save, revert ,SaveOutcome};
use crate::modes::handlers::event::EventOutcome;
use crate::pages::routing::{Router, Page};
use anyhow::Result;

pub async fn handle_command_event(
    key: KeyEvent,
    config: &Config,
    app_state: &mut AppState,
    router: &mut Router,
    command_input: &mut String,
    command_message: &mut String,
    grpc_client: &mut GrpcClient,
    command_handler: &mut CommandHandler,
    terminal: &mut TerminalCore,
    current_position: &mut u64,
    total_count: u64,
) -> Result<EventOutcome> {
    // Exit command mode
    if config.is_exit_command_mode(key.code, key.modifiers) {
        command_input.clear();
        *command_message = "".to_string();
        return Ok(EventOutcome::Ok("Exited command mode".to_string()));
    }

    // Execute command
    if config.is_command_execute(key.code, key.modifiers) {
        return process_command(
            config,
            app_state,
            router,
            command_input,
            command_message,
            grpc_client,
            command_handler,
            terminal,
            current_position,
            total_count,
        )
        .await;
    }

    // Backspace
    if config.is_command_backspace(key.code, key.modifiers) {
        command_input.pop();
        return Ok(EventOutcome::Ok("".to_string()));
    }

    // Regular character input
    if let KeyCode::Char(c) = key.code {
        if key.modifiers.is_empty() || key.modifiers == KeyModifiers::SHIFT {
            command_input.push(c);
            return Ok(EventOutcome::Ok("".to_string()));
        }
    }

    Ok(EventOutcome::Ok("".to_string()))
}

async fn process_command(
    config: &Config,
    app_state: &mut AppState,
    router: &mut Router,
    command_input: &mut String,
    command_message: &mut String,
    grpc_client: &mut GrpcClient,
    command_handler: &mut CommandHandler,
    terminal: &mut TerminalCore,
    current_position: &mut u64,
    total_count: u64,
) -> Result<EventOutcome> {
    let command = command_input.trim().to_string();
    if command.is_empty() {
        *command_message = "Empty command".to_string();
        return Ok(EventOutcome::Ok(command_message.clone()));
    }

    let action = config.get_action_for_command(&command).unwrap_or("unknown");

    match action {
        "force_quit" | "save_and_quit" | "quit" => {
            let (should_exit, message) = command_handler
                .handle_command(action, terminal, app_state, router)
                .await?;
            command_input.clear();
            if should_exit {
                Ok(EventOutcome::Exit(message))
            } else {
                Ok(EventOutcome::Ok(message))
            }
        }
        "save" => {
            if let Page::Form(path) = &router.current {
                let outcome = save(app_state, path, grpc_client).await?;
                let message = match outcome {
                    SaveOutcome::CreatedNew(_) => "New entry created".to_string(),
                    SaveOutcome::UpdatedExisting => "Entry updated".to_string(),
                    SaveOutcome::NoChange => "No changes to save".to_string(),
                };
                command_input.clear();
                Ok(EventOutcome::DataSaved(outcome, message))
            } else {
                Ok(EventOutcome::Ok("Not in a form page".to_string()))
            }
        }
        "revert" => {
            if let Page::Form(path) = &router.current {
                let message = revert(app_state, path, grpc_client).await?;
                command_input.clear();
                Ok(EventOutcome::Ok(message))
            } else {
                Ok(EventOutcome::Ok("Not in a form page".to_string()))
            }
        }
        _ => {
            let message = format!("Unhandled action: {}", action);
            command_input.clear();
            Ok(EventOutcome::Ok(message))
        }
    }
}
