pub mod cell;
pub mod commit;
pub mod core;
pub mod effect_cmds;
pub mod grid;
pub mod mode;
pub mod navigation;
pub mod panel;
pub mod registry;
pub mod search;
pub mod text_buffer;
pub mod tile;

// Re-export items used by external code
pub use self::core::{Cmd, CmdContext, CmdRegistry};
pub use registry::default_registry;

#[cfg(test)]
pub(super) mod test_helpers {
    use std::collections::HashMap;

    use crossterm::event::KeyCode;

    use crate::ui::app::AppMode;
    use crate::ui::effect::Effect;
    use crate::view::GridLayout;
    use crate::workbook::Workbook;

    use super::core::CmdContext;
    use super::registry::default_registry;

    pub type CmdRegistry = super::core::CmdRegistry;

    pub static EMPTY_BUFFERS: std::sync::LazyLock<HashMap<String, String>> =
        std::sync::LazyLock::new(HashMap::new);
    pub static EMPTY_EXPANDED: std::sync::LazyLock<std::collections::HashSet<String>> =
        std::sync::LazyLock::new(std::collections::HashSet::new);

    pub fn make_layout(workbook: &Workbook) -> GridLayout {
        GridLayout::new(&workbook.model, workbook.active_view())
    }

    pub fn make_ctx<'a>(
        workbook: &'a Workbook,
        layout: &'a GridLayout,
        registry: &'a CmdRegistry,
    ) -> CmdContext<'a> {
        let view = workbook.active_view();
        let (sr, sc) = view.selected;
        CmdContext {
            model: &workbook.model,
            workbook,
            view,
            layout,
            registry,
            mode: &AppMode::Normal,
            selected: view.selected,
            row_offset: view.row_offset,
            col_offset: view.col_offset,
            search_query: "",
            yanked: &None,
            dirty: false,
            search_mode: false,
            formula_panel_open: false,
            category_panel_open: false,
            view_panel_open: false,
            formula_cursor: 0,
            cat_panel_cursor: 0,
            view_panel_cursor: 0,
            tile_cat_idx: 0,
            buffers: &EMPTY_BUFFERS,
            view_back_stack: &[],
            view_forward_stack: &[],
            has_drill_state: false,
            display_value: {
                let key = layout.cell_key(sr, sc);
                key.as_ref()
                    .and_then(|k| workbook.model.get_cell(k).cloned())
                    .map(|v| v.to_string())
                    .unwrap_or_default()
            },
            visible_rows: 20,
            visible_cols: 8,
            expanded_cats: &EMPTY_EXPANDED,
            key_code: KeyCode::Null,
        }
    }

    pub fn two_cat_model() -> Workbook {
        let mut wb = Workbook::new("Test");
        wb.add_category("Type").unwrap();
        wb.add_category("Month").unwrap();
        wb.model.category_mut("Type").unwrap().add_item("Food");
        wb.model.category_mut("Type").unwrap().add_item("Clothing");
        wb.model.category_mut("Month").unwrap().add_item("Jan");
        wb.model.category_mut("Month").unwrap().add_item("Feb");
        wb
    }

    pub fn three_cat_model_with_page() -> Workbook {
        let mut wb = Workbook::new("Test");
        wb.add_category("Type").unwrap();
        wb.add_category("Month").unwrap();
        wb.add_category("Region").unwrap();
        wb.model.category_mut("Type").unwrap().add_item("Food");
        wb.model.category_mut("Type").unwrap().add_item("Clothing");
        wb.model.category_mut("Month").unwrap().add_item("Jan");
        wb.model.category_mut("Month").unwrap().add_item("Feb");
        wb.model.category_mut("Region").unwrap().add_item("North");
        wb.model.category_mut("Region").unwrap().add_item("South");
        wb.model.category_mut("Region").unwrap().add_item("East");
        wb.active_view_mut()
            .set_axis("Region", crate::view::Axis::Page);
        wb
    }

    pub fn effects_debug(effects: &[Box<dyn Effect>]) -> String {
        format!("{:?}", effects)
    }

    pub fn make_registry() -> CmdRegistry {
        default_registry()
    }
}
