package com.tencent.liteav.demo.superplayer.database.repo

import com.tencent.liteav.demo.superplayer.database.dao.CountDownDao
import com.tencent.liteav.demo.superplayer.database.dao.HistoryDao
import com.tencent.liteav.demo.superplayer.database.entity.CountDown
import com.tencent.liteav.demo.superplayer.database.entity.History
import kotlinx.coroutines.flow.Flow
import java.text.SimpleDateFormat
import java.util.*

public class PlayerRepository(
    private val historyDao: HistoryDao,
    private val countDownDao: CountDownDao,
) {
    companion object {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE)
    }

    suspend fun insertHistory(history: History) {
        historyDao.insertHistory(history)
    }

    suspend fun insertCountDown(countDown: CountDown) {
        countDownDao.insertCountDown(countDown)
    }

    suspend fun findHasCountDown(courseId: String): Flow<CountDown?> {
        return countDownDao.getCountDownByCourseId(courseId)
    }

    suspend fun queryTodayEpisodeHistory(): Flow<Int> {
        val date = dateFormat.format(Date())
        return historyDao.queryTodayEpisodeHistory(date)
    }

    suspend fun queryTodayDurationHistory(): Flow<List<History>> {
        val date = dateFormat.format(Date())
        return historyDao.queryTodayDurationHistory(date)
    }
}