package com.joyful.euphoria.board.assistant

import com.joyful.euphoria.board.beaned.BeanDetails
import com.joyful.euphoria.board.database.BoardDataBase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

object DatabaseAssistant {


    suspend fun addLike(data: BeanDetails) {
        withContext(Dispatchers.IO) {
            BoardDataBase.Companion.boardDataBase.ThemesDao().insertData(data)
        }

    }
    suspend fun removeLike(data: BeanDetails) {
        withContext(Dispatchers.IO) {
            val queryIsLike =
                BoardDataBase.Companion.boardDataBase.ThemesDao().queryIsLike(data.titleName)
            if (queryIsLike != null) {
                BoardDataBase.Companion.boardDataBase.ThemesDao().delete(queryIsLike)
            }
        }

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

    }
}