package com.example.myapplication.database

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.myapplication.R
import com.example.myapplication.database.dao.CategoryDao
import com.example.myapplication.database.dao.ProductDao
import com.example.myapplication.database.dao.RemoteKeysDao
import com.example.myapplication.database.dao.UserDao
import com.example.myapplication.database.entities.Category
import com.example.myapplication.database.entities.Converters
import com.example.myapplication.database.entities.Product
import com.example.myapplication.database.entities.RemoteKeys

import com.example.myapplication.database.entities.User
import com.example.myapplication.database.entities.UserProductCart

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Database(entities = [User::class, Product::class, Category:: class, UserProductCart::class , RemoteKeys::class], version = 1, exportSchema = false)
@TypeConverters(Converters::class)
abstract class AppDb: RoomDatabase(){
    abstract fun userDao(): UserDao
    abstract fun productDao(): ProductDao
    abstract fun categoryDao(): CategoryDao
    abstract fun remoteKeysDao(): RemoteKeysDao

    companion object {
        private const val DB_NAME: String = "myApp2.db"

        @Volatile
        private var INSTANCE: AppDb? = null
        private suspend fun populateDatabase(context: Context) {
            INSTANCE?.let { database ->

                val categoryDao = database.categoryDao()

//                categoryDao.insert(Category(1, "Видеокарты"))
//                categoryDao.insert(Category(2, "Процессоры"))
//                categoryDao.insert(Category(3, "Оперативная память"))
//                categoryDao.insert(Category(4, "Твердотельные накопители"))

                val productDao = database.productDao()

                val img1: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.product1)
//                val img2: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.product2)
//                val img3: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.product3)
//                val img4: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.product4)
//                val img5: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.product5)
//                val img6: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.product6)
//                val img7: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.product7)
//                val img8: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.product8)

                productDao.insert(Product(1, "MSI GeForce RTX 4090 VENTUS 3X OC",
                    "Информацио о товаре MSI GeForce RTX 4090 VENTUS 3X OC ",
                    210999.0, img1, 1
                    ))
//
//                productDao.insert(Product(2, "Palit GeForce GTX 1660 SUPER",
//                    "Информацио о товаре Palit GeForce GTX 1660 SUPER ",
//                    25999.0, img2, 1
//                ))
//
//                productDao.insert(Product(3, "Intel Celeron G5905 OEM",
//                    "Информацио о товаре Intel Celeron G5905 OEM ",
//                    25999.0, img3, 2
//                ))
//
//
//                productDao.insert(Product(4, "AMD Ryzen 5 4500 BOX",
//                    "Информацио о товаре Intel Celeron G5905 OEM",
//                    9799.0, img4, 2
//                ))
//
//                productDao.insert(Product(5, "Kingston FURY Beast Black",
//                    "Информацио о товаре Kingston FURY Beast Black",
//                    4499.0, img5, 3
//                ))
//
//                productDao.insert(Product(6, "ADATA XPG SPECTRIX D41 RGB",
//                    "Информацио о товаре ADATA XPG SPECTRIX D41 RGB",
//                    4599.0, img6, 3
//                ))
//
//                productDao.insert(Product(7, "ADATA SU650",
//                    "Информацио о товаре ADATA SU650",
//                    1550.0, img7, 4
//                ))
//                productDao.insert(Product(8, "Smartbuy Revival 3",
//                    "Информацио о товаре Smartbuy Revival 3",
//                    1250.0, img8, 4
//                ))

                  val userDao = database.userDao()
                    userDao.insert(User(5, "Иванов И.И", "ivanov","ivanov"))
//                database.userDao().addProductCart(UserProductCart(1, 1))
//                database.userDao().addProductCart(UserProductCart(1, 3))
//                database.userDao().addProductCart(UserProductCart(1, 2))
            }
        }
        fun getInstance(appContext: Context): AppDb {
            return INSTANCE ?: synchronized(this) {
                Room.databaseBuilder(
                    appContext,
                    AppDb::class.java,
                    DB_NAME
                )
                    .addCallback(object : Callback() {
                        override fun onCreate(db: SupportSQLiteDatabase) {
                            super.onCreate(db)
                            CoroutineScope(Dispatchers.IO).launch {
                                populateDatabase(appContext)
                            }
                        }
                    })
                    .build()
                    .also { INSTANCE = it }
            }
        }
    }
}
