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

import android.util.Log
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
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.dao.RecordDao
import com.tencent.liteav.demo.superplayer.database.entity.CountDown
import com.tencent.liteav.demo.superplayer.database.entity.History
import com.tencent.liteav.demo.superplayer.database.entity.Record
import kotlinx.coroutines.flow.Flow
import java.text.SimpleDateFormat
import java.util.*

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

    /**
     * 定时相关的数据库操作
     */
    fun insertCountDown(countDown: CountDown) {
        countDownDao.insertCountDown(countDown)
    }

    fun deleteCountDown(courseId: String) {
        countDownDao.deleteCountDown(courseId)
    }

    fun findHasCountDown(courseId: String): CountDown? {
        return countDownDao.queryCountDown(courseId)
    }


    /**
     * 历史记录有关的操作
     */
    fun insertHistory(history: History) {
        historyDao.insertHistory(history)
    }

    fun queryTodayEpisodeHistory(
        courseId: String,
        countDown: String,
    ): Int {
        val date = dateFormat.format(Date())
        return historyDao.queryTodayEpisodeHistory(courseId, countDown, date)
    }

    fun queryTodayDurationHistory(
        courseId: String,
        countDown: String,
    ): List<History> {
        val date = dateFormat.format(Date())
        return historyDao.queryTodayDurationHistory(courseId, countDown, date)
    }

    // 先查询保存了哪几个小节的记录
    fun queryAllHistory(courseId: String): List<String> {
        return historyDao.queryAllHistory(courseId)
    }

    // 再依次查询每小节对应的记录
    fun queryAllSectionHistory(courseId: String, sectionId: String): List<History> {
        return historyDao.queryAllSectionHistory(courseId, sectionId)
    }

    fun deleteAllHistory(courseId: String) {
        historyDao.deleteAllHistory(courseId)
    }

    fun queryRecord(courseId: String): List<Record> {
        return recordDao.queryRecord(courseId)
    }

    fun queryRecord(courseId: String, sectionId: String): Record? {
        return recordDao.queryRecord(courseId, sectionId)
    }

    fun insertRecord(record: Record) {
        recordDao.insertRecord(record)
    }

    fun deleteRecord(courseId: String) {
        recordDao.deleteRecord(courseId)
    }
}