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

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kr.co.vividnext.sodalive.mypage.alarm.db.AlarmDatabase
import kr.co.vividnext.sodalive.mypage.alarm.scheduler.AlarmScheduler

class AlarmBootReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
            val alarmDao = AlarmDatabase.getDatabase(context).alarmDao()
            val scheduler = AlarmScheduler(context)

            CoroutineScope(Dispatchers.IO).launch {
                alarmDao.getAllAlarms().collect { alarms ->
                    alarms.forEach { alarm ->
                        scheduler.setAlarm(alarm)
                    }
                }
            }
        }
    }
}
