package com.example.timert.data.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.timert.data.dao.ExerciseDao
import com.example.timert.data.entity.Exercise
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

@Database(entities = [Exercise::class], version = 1, exportSchema = false)
abstract class ExerciseDatabase:RoomDatabase(){
    abstract fun exerciseDao():ExerciseDao

   // private class ExerciseDatabaseCallback (private val scope: CoroutineScope) : RoomDatabase.Callback(){

        //override fun onOpen(db: SupportSQLiteDatabase) {
         //   super.onOpen(db)
        //    INSTANCE?.let { database ->
         //       scope.launch {
         //           populateDatabase(database.exerciseDao())
           //     }
          //  }
       // }
        //suspend fun populateDatabase(exerciseDao: ExerciseDao) {
            // Delete all content here.
            //exerciseDao.deleteAll()
            // Add sample words.
            //var exercise = Exercise(0, "training", 10, 45 )
           // exerciseDao.insert(exercise)

            // TODO: Add your own words!
     //   }
    //}
    companion object {
        @Volatile
        private var INSTANCE: ExerciseDatabase? = null
        fun getDatabase(context: Context,
                        scope: CoroutineScope
        ): ExerciseDatabase {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    ExerciseDatabase::class.java,
                    "exercise_database"
                )
                    //.addCallback(ExerciseDatabaseCallback(scope))
                    .build()
                INSTANCE = instance
                return instance
            }

        }
        }
}
