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

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
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

@Database(
    version = 1,
    entities = [History::class, CountDown::class],
    exportSchema = false
)
abstract class PlayerDatabase : RoomDatabase() {
    abstract fun historyDao(): HistoryDao
    abstract fun countDownDao(): CountDownDao

    companion object {
        // Singleton prevents multiple instances of database opening at the
        // same time.
        @Volatile
        private var INSTANCE: PlayerDatabase? = null

        @JvmStatic
        fun getDatabase(context: Context): PlayerDatabase {
            // if the INSTANCE is not null, then return it,
            // if it is, then create the database
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    PlayerDatabase::class.java,
                    "player_database"
                ).build()
                INSTANCE = instance
                // return instance
                instance
            }
        }
    }
}