package com.pdf.base.function

import android.util.Log
import com.compdfkit.core.document.CPDFDocument
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 * @author: hubowen
 * @date: 2022/9/20
 * @description:
 */

/**
 * 删除所有注释
 */
suspend fun CPDFDocument.annotationDeleteAll(onPre: (() -> Unit)? = null, onSuccess: ((Boolean) -> Unit)? = null, onComplete: (() -> Unit)? = null) {
    withContext(Dispatchers.Main) {
        onPre?.invoke()
        withContext(Dispatchers.IO) {
            runCatching {
                //删除所有注释
                removeAllAnnotations()
                //保存Document
                onSaveDocument()
            }.onFailure {
                Log.e("pdf_base", "delete_all_annotation_failed")
                onSuccess?.invoke(false)
            }.onSuccess {
                Log.e("pdf_base", "delete_all_annotation_success")
                onSuccess?.invoke(true)
            }
        }
        onComplete?.invoke()
    }
}