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

//! DocumentIo feature commands

use crate::app_context::AppContext;
use anyhow::{Context, Result};
use document_io::{
    ExportDocxDto, ExportDocxResultDto, ExportHtmlDto, ExportLatexDto, ExportLatexResultDto,
    ExportMarkdownDto, ExportPlainTextDto, ImportHtmlDto, ImportHtmlResultDto, ImportMarkdownDto,
    ImportMarkdownResultDto, ImportPlainTextDto, document_io_controller,
};

use common::long_operation::OperationProgress;

pub fn import_plain_text(ctx: &AppContext, dto: &ImportPlainTextDto) -> Result<()> {
    document_io_controller::import_plain_text(&ctx.db_context, &ctx.event_hub, dto)
        .context("import_plain_text")
}

pub fn export_plain_text(ctx: &AppContext) -> Result<ExportPlainTextDto> {
    document_io_controller::export_plain_text(&ctx.db_context, &ctx.event_hub)
        .context("export_plain_text")
}

/// import_markdown (long operation)
pub fn import_markdown(ctx: &AppContext, dto: &ImportMarkdownDto) -> Result<String> {
    document_io_controller::import_markdown(
        &ctx.db_context,
        &ctx.event_hub,
        &mut ctx.long_operation_manager.lock().unwrap(),
        dto,
    )
    .context("import_markdown")
}

/// Get the progress of a import_markdown operation
pub fn get_import_markdown_progress(
    ctx: &AppContext,
    operation_id: &str,
) -> Option<OperationProgress> {
    document_io_controller::get_import_markdown_progress(
        &ctx.long_operation_manager.lock().unwrap(),
        operation_id,
    )
}

/// Get the result of a import_markdown operation
pub fn get_import_markdown_result(
    ctx: &AppContext,
    operation_id: &str,
) -> Result<Option<ImportMarkdownResultDto>> {
    document_io_controller::get_import_markdown_result(
        &ctx.long_operation_manager.lock().unwrap(),
        operation_id,
    )
    .context("getting import_markdown result")
}

pub fn export_markdown(ctx: &AppContext) -> Result<ExportMarkdownDto> {
    document_io_controller::export_markdown(&ctx.db_context, &ctx.event_hub)
        .context("export_markdown")
}

/// import_html (long operation)
pub fn import_html(ctx: &AppContext, dto: &ImportHtmlDto) -> Result<String> {
    document_io_controller::import_html(
        &ctx.db_context,
        &ctx.event_hub,
        &mut ctx.long_operation_manager.lock().unwrap(),
        dto,
    )
    .context("import_html")
}

/// Get the progress of a import_html operation
pub fn get_import_html_progress(ctx: &AppContext, operation_id: &str) -> Option<OperationProgress> {
    document_io_controller::get_import_html_progress(
        &ctx.long_operation_manager.lock().unwrap(),
        operation_id,
    )
}

/// Get the result of a import_html operation
pub fn get_import_html_result(
    ctx: &AppContext,
    operation_id: &str,
) -> Result<Option<ImportHtmlResultDto>> {
    document_io_controller::get_import_html_result(
        &ctx.long_operation_manager.lock().unwrap(),
        operation_id,
    )
    .context("getting import_html result")
}

pub fn export_html(ctx: &AppContext) -> Result<ExportHtmlDto> {
    document_io_controller::export_html(&ctx.db_context, &ctx.event_hub).context("export_html")
}

pub fn export_latex(ctx: &AppContext, dto: &ExportLatexDto) -> Result<ExportLatexResultDto> {
    document_io_controller::export_latex(&ctx.db_context, &ctx.event_hub, dto)
        .context("export_latex")
}

/// export_docx (long operation)
pub fn export_docx(ctx: &AppContext, dto: &ExportDocxDto) -> Result<String> {
    document_io_controller::export_docx(
        &ctx.db_context,
        &ctx.event_hub,
        &mut ctx.long_operation_manager.lock().unwrap(),
        dto,
    )
    .context("export_docx")
}

/// Get the progress of a export_docx operation
pub fn get_export_docx_progress(ctx: &AppContext, operation_id: &str) -> Option<OperationProgress> {
    document_io_controller::get_export_docx_progress(
        &ctx.long_operation_manager.lock().unwrap(),
        operation_id,
    )
}

/// Get the result of a export_docx operation
pub fn get_export_docx_result(
    ctx: &AppContext,
    operation_id: &str,
) -> Result<Option<ExportDocxResultDto>> {
    document_io_controller::get_export_docx_result(
        &ctx.long_operation_manager.lock().unwrap(),
        operation_id,
    )
    .context("getting export_docx result")
}
