// Generated by Qleany v1.5.1 from frontend_feature_commands.tera

//! DocumentEditing feature commands

use crate::app_context::AppContext;
use anyhow::{Context, Result};
use document_editing::{
    AddBlockToListDto, CreateListDto, CreateListResultDto, DeleteTextDto, DeleteTextResultDto,
    InsertBlockDto, InsertBlockResultDto, InsertFormattedTextDto, InsertFormattedTextResultDto,
    InsertFragmentDto, InsertFragmentResultDto, InsertFrameDto, InsertFrameResultDto,
    InsertHtmlAtPositionDto, InsertHtmlAtPositionResultDto, InsertImageDto, InsertImageResultDto,
    InsertListDto, InsertListResultDto, InsertMarkdownAtPositionDto,
    InsertMarkdownAtPositionResultDto, InsertTableColumnDto, InsertTableColumnResultDto,
    InsertTableDto, InsertTableResultDto, InsertTableRowDto, InsertTableRowResultDto,
    InsertTextDto, InsertTextResultDto, MergeTableCellsDto, MergeTableCellsResultDto,
    RemoveBlockFromListDto, RemoveTableColumnDto, RemoveTableColumnResultDto, RemoveTableDto,
    RemoveTableRowDto, RemoveTableRowResultDto, SplitTableCellDto, SplitTableCellResultDto,
    document_editing_controller,
};

pub fn insert_text(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &InsertTextDto,
) -> Result<InsertTextResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::insert_text(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("insert_text")
}

pub fn delete_text(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &DeleteTextDto,
) -> Result<DeleteTextResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::delete_text(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("delete_text")
}

pub fn insert_block(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &InsertBlockDto,
) -> Result<InsertBlockResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::insert_block(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("insert_block")
}

pub fn insert_image(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &InsertImageDto,
) -> Result<InsertImageResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::insert_image(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("insert_image")
}

pub fn insert_frame(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &InsertFrameDto,
) -> Result<InsertFrameResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::insert_frame(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("insert_frame")
}

pub fn insert_formatted_text(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &InsertFormattedTextDto,
) -> Result<InsertFormattedTextResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::insert_formatted_text(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("insert_formatted_text")
}

pub fn create_list(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &CreateListDto,
) -> Result<CreateListResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::create_list(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("create_list")
}

pub fn insert_list(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &InsertListDto,
) -> Result<InsertListResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::insert_list(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("insert_list")
}

pub fn insert_fragment(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &InsertFragmentDto,
) -> Result<InsertFragmentResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::insert_fragment(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("insert_fragment")
}

pub fn insert_html_at_position(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &InsertHtmlAtPositionDto,
) -> Result<InsertHtmlAtPositionResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::insert_html_at_position(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("insert_html_at_position")
}

pub fn insert_markdown_at_position(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &InsertMarkdownAtPositionDto,
) -> Result<InsertMarkdownAtPositionResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::insert_markdown_at_position(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("insert_markdown_at_position")
}

pub fn insert_table(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &InsertTableDto,
) -> Result<InsertTableResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::insert_table(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("insert_table")
}

pub fn remove_table(ctx: &AppContext, stack_id: Option<u64>, dto: &RemoveTableDto) -> Result<()> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::remove_table(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("remove_table")
}

pub fn insert_table_row(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &InsertTableRowDto,
) -> Result<InsertTableRowResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::insert_table_row(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("insert_table_row")
}

pub fn insert_table_column(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &InsertTableColumnDto,
) -> Result<InsertTableColumnResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::insert_table_column(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("insert_table_column")
}

pub fn remove_table_row(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &RemoveTableRowDto,
) -> Result<RemoveTableRowResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::remove_table_row(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("remove_table_row")
}

pub fn remove_table_column(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &RemoveTableColumnDto,
) -> Result<RemoveTableColumnResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::remove_table_column(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("remove_table_column")
}

pub fn merge_table_cells(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &MergeTableCellsDto,
) -> Result<MergeTableCellsResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::merge_table_cells(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("merge_table_cells")
}

pub fn split_table_cell(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &SplitTableCellDto,
) -> Result<SplitTableCellResultDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::split_table_cell(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("split_table_cell")
}

pub fn add_block_to_list(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &AddBlockToListDto,
) -> Result<()> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::add_block_to_list(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("add_block_to_list")
}

pub fn remove_block_from_list(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &RemoveBlockFromListDto,
) -> Result<()> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    document_editing_controller::remove_block_from_list(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("remove_block_from_list")
}
