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

import android.app.Application
import com.tencent.liteav.demo.superplayer.database.PlayerDatabase.Companion.getDatabase
import com.tencent.liteav.demo.superplayer.database.repo.PlayerRepository

interface PlayerDatabaseProvider {
    fun getPlayerDatabase(): PlayerDatabase
    fun getPlayerRepository(): PlayerRepository

    public class Instance(
        val context: Application,
    ) : PlayerDatabaseProvider {
        private var database: PlayerDatabase? = null
        private var repository: PlayerRepository? = null

        override fun getPlayerDatabase(): PlayerDatabase {
            if (database == null) {
                database = getDatabase(context)
            }

            return database!!
        }

        override fun getPlayerRepository(): PlayerRepository {
            if (repository == null) {
                repository = PlayerRepository(
                    getPlayerDatabase().historyDao(),
                    getPlayerDatabase().countDownDao(),
                    getPlayerDatabase().recordDao()
                )
            }
            return repository!!
        }

    }
}