package kr.co.vividnext.sodalive.mypage.alarm

import androidx.lifecycle.LiveData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kr.co.vividnext.sodalive.mypage.alarm.db.Alarm
import kr.co.vividnext.sodalive.mypage.alarm.db.AlarmDao

class AlarmRepository(private val alarmDao: AlarmDao) {
    val allAlarms = alarmDao.getAllAlarms()

    suspend fun insert(alarm: Alarm): Alarm {
        return withContext(Dispatchers.IO) {
            alarmDao.insertAndReturnAlarm(alarm)
        }
    }

    suspend fun update(alarm: Alarm) {
        withContext(Dispatchers.IO) {
            alarmDao.updateAlarm(alarm)
        }
    }

    suspend fun delete(alarm: Alarm) {
        withContext(Dispatchers.IO) {
            alarmDao.deleteAlarm(alarm)
        }
    }

    suspend fun truncate() {
        withContext(Dispatchers.IO) {
            alarmDao.truncateTable()
        }
    }

    fun getAlarmById(id: Int): LiveData<Alarm> {
        return alarmDao.getAlarmById(id)
    }
}
