package com.keyboard.bubble.skin.keyboard.db

import com.keyboard.bubble.skin.keyboard.bean.ChildBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

object DbOpManager {


    suspend fun addLike(data: ChildBean) {
        data.isLike = true
        withContext(Dispatchers.IO) {
            MyDb.myDb.ThemesDao().insertData(data)
        }

    }

    suspend fun addDownload(data: ChildBean) {
        data.isDownload = true
        withContext(Dispatchers.IO) {
            MyDb.myDb.ThemesDao().insertData(data)
        }

    }

    suspend fun removeLike(data: ChildBean) {
        withContext(Dispatchers.IO) {
            val queryIsLike = MyDb.myDb.ThemesDao().queryIsLike(data.titleName)
            if (queryIsLike != null) {
                if (!queryIsLike.isDownload) {
                    MyDb.myDb.ThemesDao().delete(queryIsLike)
                } else {
                    queryIsLike.isLike = false
                    MyDb.myDb.ThemesDao().updateLike(queryIsLike)
                }
            }
        }

    }


    suspend fun getIsLike(name: String, action: (isLike: Boolean) -> Unit) {
        withContext(Dispatchers.IO) {
            val query = MyDb.myDb.ThemesDao().queryIsLike(name)
            withContext(Dispatchers.Main) {
                action.invoke(query != null)
            }
        }

    }
}