package com.app.input.personalised.board.basedata

import com.app.input.personalised.board.databean.KeyboardTheme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

object ThemeRepository {


    suspend fun addLike(data: KeyboardTheme) {
        withContext(Dispatchers.IO) {
            AppDatabase.boartAppDatabase.ThemesDao().insertData(data)
        }

    }
    suspend fun removeLike(data: KeyboardTheme) {
        withContext(Dispatchers.IO) {
            val queryIsLike = AppDatabase.boartAppDatabase.ThemesDao().queryIsLike(data.titleName)
            if (queryIsLike != null) {
                AppDatabase.boartAppDatabase.ThemesDao().delete(queryIsLike)
            }
        }

    }
    
    // Java 友好的方法，用于从 Java 代码调用
    fun removeLikeFromJava(data: KeyboardTheme) {
        GlobalScope.launch(Dispatchers.IO) {
            removeLike(data)
        }
    }
    suspend fun getIsLike(name: String, action: (isLike: Boolean) -> Unit) {
        withContext(Dispatchers.IO) {
            val query = AppDatabase.boartAppDatabase.ThemesDao().queryIsLike(name)
            withContext(Dispatchers.Main) {
                action.invoke(query != null)
            }
        }

    }
}