// src/modes/handlers/mode_manager.rs
use crate::state::app::state::AppState;
use crate::modes::handlers::event::EventHandler;
use crate::state::pages::add_logic::AddLogicFocus;
use crate::state::app::highlight::HighlightState;
use crate::state::pages::admin::AdminState;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AppMode {
    General,   // For intro and admin screens
    ReadOnly,  // Canvas read-only mode
    Edit,      // Canvas edit mode
    Highlight, // Cnavas highlight/visual mode
    Command,   // Command mode overlay
}

pub struct ModeManager;

impl ModeManager {
    // Determine current mode based on app state
    pub fn derive_mode(
        app_state: &AppState,
        event_handler: &EventHandler,
        admin_state: &AdminState,
    ) -> AppMode {
        if event_handler.navigation_state.active {
            return AppMode::General;
        }

        if event_handler.command_mode {
            return AppMode::Command;
        }

        if !matches!(event_handler.highlight_state, HighlightState::Off) {
            return AppMode::Highlight;
        }

        let is_canvas_view = app_state.ui.show_login
            || app_state.ui.show_register
            || app_state.ui.show_form
            || app_state.ui.show_add_table
            || app_state.ui.show_add_logic;

        if app_state.ui.show_add_logic {
            // Specific logic for AddLogic view
            match admin_state.add_logic_state.current_focus {
                AddLogicFocus::InputLogicName
                | AddLogicFocus::InputTargetColumn
                | AddLogicFocus::InputDescription => {
                    // These are canvas inputs
                    if event_handler.is_edit_mode {
                        AppMode::Edit
                    } else {
                        AppMode::ReadOnly
                    }
                }
                _ => AppMode::General,
            }
        } else if app_state.ui.show_add_table {
            if app_state.ui.focus_outside_canvas {
                AppMode::General
            } else {
                if event_handler.is_edit_mode { 
                    AppMode::Edit 
                } else { 
                    AppMode::ReadOnly 
                }
            }
        } else if is_canvas_view {
            if app_state.ui.focus_outside_canvas {
                AppMode::General
            } else {
                if event_handler.is_edit_mode {
                    AppMode::Edit
                } else {
                    AppMode::ReadOnly
                }
            }
        } else {
            AppMode::General
        }
    }

    // Mode transition rules
pub fn can_enter_command_mode(current_mode: AppMode) -> bool {
        !matches!(current_mode, AppMode::Edit)
    }

    pub fn can_enter_edit_mode(current_mode: AppMode) -> bool {
        matches!(current_mode, AppMode::ReadOnly)
    }

    pub fn can_enter_read_only_mode(current_mode: AppMode) -> bool {
        matches!(current_mode, AppMode::Edit | AppMode::Command | AppMode::Highlight)
    }

    pub fn can_enter_highlight_mode(current_mode: AppMode) -> bool {
        matches!(current_mode, AppMode::ReadOnly)
    }
}
