package com.ux.video.file.filerecovery.db

import android.content.Context
import androidx.lifecycle.LifecycleCoroutineScope
import com.ux.video.file.filerecovery.utils.Common
import com.ux.video.file.filerecovery.utils.FileType

import io.objectbox.Box
import io.objectbox.BoxStore
import io.objectbox.config.DebugFlags
import io.objectbox.reactive.DataSubscription
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.jvm.java

object ObjectBoxManager {
    val pageSize = 10

    lateinit var objectboxStore: BoxStore


    var AllRecoveryBox: Box<ResultDataFiles>? = null


    val observer: DataSubscription? = null


    fun initObjectBoxDb(context: Context) {
        objectboxStore = MyObjectBox.builder().androidContext(context)
            .debugFlags(DebugFlags.LOG_QUERIES or DebugFlags.LOG_QUERY_PARAMETERS)
            .build()

    }

    private fun getDataBox(): Box<ResultDataFiles> {
        AllRecoveryBox =
            AllRecoveryBox ?: objectboxStore.boxFor<ResultDataFiles>(ResultDataFiles::class.java)

        return AllRecoveryBox!!
    }

    //    fun getLikeBox(): Box<ResultPhotosFiles>? {
//        likeWallpaperBox = likeWallpaperBox ?: boxStore?.boxFor<LikeWallpaper>(ResultPhotosFiles::class.java)
//        return likeWallpaperBox
//    }
//
//    fun setLikeUpdateListener(queryAllLike:(List<LikeWallpaper>)->Unit): DataSubscription {
//        val likeBox: Box<LikeWallpaper>? = getLikeBox()
//        val build: Query<LikeWallpaper> = likeBox!!.query()
//            .build()
//        return build.subscribe(DataSubscriptionList())
//            .on(AndroidScheduler.mainThread())
//            .observer { data ->
//                Helper.showMyLog("---OnLikeUpdateListener-------------" + data.size)
//                queryAllLike(data)
//            }
//    }
//
    fun addRecoveryFile(resultData: ResultDataFiles) {
        val objectBox: Box<ResultDataFiles> = getDataBox()
        val first = objectBox.query(ResultDataFiles_.path.equal(resultData.path))
            .build().findFirst()
        if (first == null) {
            objectBox.put(resultData)
            Common.showLog("----------addRecoveryFile----${resultData.path}")
        }
    }

    //
//
//    fun insertLike(favoriteData: LikeWallpaper) {
//        val likeBox: Box<LikeWallpaper>? = getLikeBox()
//        val first: LikeWallpaper? = likeBox!!.query()
//            .equal(LikeWallpaper_.id, favoriteData.id)
//            .build()
//            .findFirst()
//        if (first == null) {
//            Helper.showMyLog("---insertLike-----------------id=" + favoriteData.id)
//            likeBox.put(favoriteData)
//        } else {
//            Helper.showMyLog("---insertLike-----------------id=" + favoriteData.id)
//        }
//    }
//
//
    fun deleteRecoveryFile(resultData: ResultDataFiles) {
        val objectBox = getDataBox()
        val first =
            objectBox.query(ResultDataFiles_.path.equal(resultData.path)).build().findFirst()

        if (first != null) {
            val remove = objectBox.remove(first.id)
            Common.showLog("----------addRecoveryFile---删除${remove}-${resultData.path}")
        }
    }




    fun  deleteRecoveryFilesAsyncById(lifecycleCoroutineScope: LifecycleCoroutineScope,list: List<ResultDataFiles>, onComplete: () -> Unit) {
        lifecycleCoroutineScope.launch(Dispatchers.IO) {
            val objectBox = getDataBox()
            val ids = list.map { it.id }
            objectBox.removeByIds(ids)
            withContext(Dispatchers.Main) {
                onComplete()
            }
        }
    }


    //
//    fun queryIsLike(id: Long): Boolean {
//        val likeBox: Box<LikeWallpaper>? = getLikeBox()
//        val first: LikeWallpaper? = likeBox!!.query()
//            .equal(LikeWallpaper_.id, id)
//            .build()
//            .findFirst()
//        return first != null
//    }
//
//
//
    fun queryRecoveryFile(type: Int): List<ResultDataFiles> {
        val dataBox = getDataBox()
        val list: List<ResultDataFiles> = dataBox.query(ResultDataFiles_.fileType.equal(type))
            .build()
            .find()

        return list
    }

    suspend fun queryRecoveryFileAsync(type: FileType): List<ResultDataFiles> =
        withContext(Dispatchers.IO) {
            val dataBox = getDataBox()
            dataBox.query(ResultDataFiles_.fileType.equal(type.value))
                .build()
                .find()
        }

//
//    fun queryLike(currentPage: Int): List<LikeWallpaper> {
//        val offset: Int = (currentPage - 1) * pageSize
//        val likeBox: Box<LikeWallpaper>? = getLikeBox()
//        val data: List<LikeWallpaper> = likeBox!!.query()
//            .build()
//            .find(offset.toLong(), pageSize.toLong())
//
//        return data
//    }
//
//    fun queryAllData(wallpaperType: Long): List<VideoWallpaper> {
//        val objectBoxLike: Box<VideoWallpaper>? = getDataBox()
//        val data: List<VideoWallpaper> = objectBoxLike!!.query()
//            .equal(VideoWallpaper_.wallpapertype, wallpaperType)
//            .build()
//            .find()
//
//        return data
//    }
}