package com.zyzf.coffeepreorder.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.zyzf.coffeepreorder.database.dao.CartDao
import com.zyzf.coffeepreorder.database.dao.CoffeeDao
import com.zyzf.coffeepreorder.database.dao.RemoteKeysDao
import com.zyzf.coffeepreorder.database.dao.UserDao
import com.zyzf.coffeepreorder.database.model.Cart
import com.zyzf.coffeepreorder.database.model.Coffee
import com.zyzf.coffeepreorder.database.model.RemoteKeys
import com.zyzf.coffeepreorder.database.model.User

@Database(entities = [User::class, Coffee::class, Cart::class, RemoteKeys::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
    abstract fun coffeeDao(): CoffeeDao
    abstract fun cartDao(): CartDao
    abstract fun remoteKeysDao(): RemoteKeysDao

    companion object {
        private const val DB_NAME: String = "coffee-preorder"

        @Volatile
        private var INSTANCE: AppDatabase? = null

        private suspend fun populateDatabase() {
            INSTANCE?.let { database ->
                // Users
                val userDao = database.userDao()
                val user1 = User("zyzf", "Ян К.", "+79911152503", "250303zyzf", "admin")
                userDao.insert(user1)
                // Coffees
                val coffeeDao = database.coffeeDao()
                val coffee1 = Coffee("Coffee1", 200.0, "Ing1")
                val coffee2 = Coffee("Coffee2", 200.0, "Ing1")
                val coffee3 = Coffee("Coffee3", 300.0, "Ing1")
                val coffee4 = Coffee("Coffee4", 200.0, "Ing1")
                val coffee5 = Coffee("Coffee5", 200.0, "Ing1")
                val coffee6 = Coffee("Coffee6", 25.0, "Ing1")
                val coffee7 = Coffee("Coffee7", 400.0, "Ing1")
                val coffee8 = Coffee("Coffee8", 200.0, "Ing1")
                val coffee9 = Coffee("Coffee9", 200.0, "Ing1")
                val coffee10 = Coffee("Coffee10", 900.0, "Ing1")
                val coffee11 = Coffee("Coffee11", 200.0, "Ing1")
                coffeeDao.insert(coffee1)
                coffeeDao.insert(coffee2)
                coffeeDao.insert(coffee3)
                coffeeDao.insert(coffee4)
                coffeeDao.insert(coffee5)
                coffeeDao.insert(coffee6)
                coffeeDao.insert(coffee7)
                coffeeDao.insert(coffee8)
                coffeeDao.insert(coffee9)
                coffeeDao.insert(coffee10)
                coffeeDao.insert(coffee11)
                // Cart
                val cartDao = database.cartDao()
                val cart = Cart(2, 1)
                cartDao.insert(cart)
            }
        }

        fun getInstance(appContext: Context): AppDatabase {
            return INSTANCE ?: synchronized(this) {
                Room.databaseBuilder(
                    appContext,
                    AppDatabase::class.java,
                    DB_NAME
                )
//                    .addCallback(object : Callback() {
//                        override fun onCreate(db: SupportSQLiteDatabase) {
//                            super.onCreate(db)
//                            CoroutineScope(Dispatchers.IO).launch {
//                                populateDatabase()
//                            }
//                        }
//                    })
                    .allowMainThreadQueries()
                    .build()
                    .also { INSTANCE = it }
            }
        }
    }
}