package com.ux.video.file.filerecovery.photo

import android.app.Activity
import androidx.fragment.app.FragmentManager

import androidx.lifecycle.lifecycleScope
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: ((ResultPhotosFiles) -> Unit)? = null

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


    /**
     * 显示恢复中弹窗
     */
    fun showRecoveringDialog(
        fragmentManager: FragmentManager,
        scope: CoroutineScope,
        selectedSetList: Set<ResultPhotosFiles>,
        onComplete: (number: Int) -> Unit
    ) {
        scope.copySelectedFilesAsync(
            selectedSet = selectedSetList,
            folder = Common.recoveryPhotoDir,
            onProgress = { currentCounts: Int, fileName: String, success: Boolean ->
                ScanManager.showLog("--------恢复图片 ", "----------${currentCounts} ${fileName}")
                dialogRecovering?.updateProgress(currentCounts)

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

        }
        dialogRecovering = dialogRecovering ?: RecoveringDialogFragment()
        dialogRecovering?.run {
            total = selectedSetList.size
            completeListener = { number ->
                onComplete(number)
                dialogRecovering?.dismiss()
            }
            show(fragmentManager, "")
        }
    }

    /**
     * 显示删除中弹窗
     */
    fun showConfirmDeleteDialog(
        isInfoDelete: Boolean = false,
        fragmentManager: FragmentManager,
        scope: CoroutineScope,
        selectedSetList: Set<ResultPhotosFiles>,
        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<ResultPhotosFiles>,
        onComplete: (number: Int) -> Unit
    ) {
        scope.deleteFilesAsync(
            selectedSet = selectedSetList,
            onProgress = { currentCounts: Int, path: String, success: Boolean ->
                ScanManager.showLog("--------删除图片 ", "----------${currentCounts} ${path}")
                dialogDeleting?.updateProgress(currentCounts)
            }) { counts ->
            dialogDeleting?.updateProgress(counts)
            ScanManager.showLog("--------恢复图片 ", "----------恢复完成 ${counts}")
        }
        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, "")
        }
    }
}