package com.ux.video.file.filerecovery.sort

import androidx.fragment.app.FragmentManager
import com.ux.video.file.filerecovery.db.ObjectBoxManager
import com.ux.video.file.filerecovery.db.ResultDataFiles

import com.ux.video.file.filerecovery.utils.Common
import com.ux.video.file.filerecovery.utils.ScanManager
import com.ux.video.file.filerecovery.utils.ScanManager.copySelectedFilesAsync
import com.ux.video.file.filerecovery.utils.ScanManager.deleteFilesAsync
import kotlinx.coroutines.CoroutineScope

object RecoverOrDeleteManager {

    private var dialogRecovering: RecoveringDialogFragment? = null

    private var dialogDeleting: DeletingDialogFragment? = null
    private var dialogConfirmDelete: ConfirmDeleteDialogFragment? = null

    //详情页面进行删除操作的监听
    private var onSingleDeletedCompleteListener: ((ResultDataFiles) -> Unit)? = null

    fun setOnSingleDeleteCompleteListener(listener: (ResultDataFiles) -> Unit) {
        onSingleDeletedCompleteListener = listener
    }


    /**
     * 显示恢复中弹窗
     */
    fun showRecoveringDialog(
        fragmentManager: FragmentManager,
        scope: CoroutineScope,
        selectedSetList: Set<ResultDataFiles>,
        onComplete: (number: Int) -> Unit
    ) {

        dialogRecovering = dialogRecovering ?: RecoveringDialogFragment()
        dialogRecovering?.run {
            total = selectedSetList.size
            completeListener = { number ->
                onComplete(number)
                dialogRecovering?.dismiss()
            }
            show(fragmentManager, "")
        }
        scope.copySelectedFilesAsync(
            selectedSet = selectedSetList,
            folder = Common.recoveryPhotoDir,
            onProgress = { currentCounts: Int, data: ResultDataFiles, fileName: String, success: Boolean ->
                if(success){
                    ScanManager.showLog("--------恢复图片 ", "----------${currentCounts} ${fileName}")
                    dialogRecovering?.updateProgress(currentCounts)
                    ObjectBoxManager.addRecoveryFile(data)
                }

            }) { counts ->
            dialogRecovering?.updateProgress(counts)
            ScanManager.showLog("--------恢复图片 ", "----------恢复完成 ${counts}")

        }
    }

    /**
     * 显示删除中弹窗
     */
    fun showConfirmDeleteDialog(
        isInfoDelete: Boolean = false,
        fragmentManager: FragmentManager,
        scope: CoroutineScope,
        selectedSetList: Set<ResultDataFiles>,
        onComplete: (number: Int) -> Unit
    ) {
        dialogConfirmDelete = dialogConfirmDelete ?: ConfirmDeleteDialogFragment()
        dialogConfirmDelete?.run {
            onClickDelete = {
                showDeletingDialog(
                    isInfoDelete,
                    fragmentManager,
                    scope,
                    selectedSetList,
                    onComplete
                )
            }
            show(fragmentManager, "")
        }
    }

    private fun showDeletingDialog(
        isInfoDelete: Boolean = false,
        fragmentManager: FragmentManager,
        scope: CoroutineScope,
        selectedSetList: Set<ResultDataFiles>,
        onComplete: (number: Int) -> Unit
    ) {

        dialogDeleting = dialogDeleting ?: DeletingDialogFragment()
        dialogDeleting?.run {
            total = selectedSetList.size
            completeListener = { number ->
                dialogDeleting?.dismiss()
                onComplete(number)
                if (isInfoDelete && selectedSetList.size == 1) {
                    onSingleDeletedCompleteListener?.invoke(selectedSetList.first())
                }
            }
            show(fragmentManager, "")
        }
        scope.deleteFilesAsync(
            selectedSet = selectedSetList,
            onProgress = { currentCounts: Int, data: ResultDataFiles, path: String, success: Boolean ->
                if (success){
                    ScanManager.showLog("--------删除图片 ", "----------${currentCounts} ${path}")
                    dialogDeleting?.updateProgress(currentCounts)

                }

            }) { counts ->
            dialogDeleting?.updateProgress(counts)
            ScanManager.showLog("--------恢复图片 ", "----------恢复完成 ${counts}")
        }
    }
}