package com.example.bnsptest.data.repository

import androidx.lifecycle.LiveData
import com.example.bnsptest.data.local.dao.ProductDao
import com.example.bnsptest.data.local.dao.Product
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class ProductRepository(private val productDao: ProductDao) {
    val allProducts: LiveData<List<Product>> = productDao.getAllProducts()

    suspend fun insertInitialProducts() {
        withContext(Dispatchers.IO) {
            val count = productDao.getProductCount()
            if (count == 0) {
                val dummyProducts = listOf(
                    Product(
                        "1",
                        "Headphone",
                        "Great bass and best sound",
                        100000,
                        "https://i.pinimg.com/236x/5d/59/60/5d59606b94a61b6459c11172e1fede7e.jpg",
                        100
                    ),
                    Product(
                        "2",
                        "Spicy Sausage",
                        "Yeah its spicy",
                        10000,
                        "https://www.static-src.com/wcsstore/Indraprastha/images/catalog/full//104/MTA-44694473/kanzler_sausage_singles_kanzler_hot_65gr_sosis_sapi_single_pedas_halal_full03_hzxu5ah0.jpg",
                        50
                    ),
                    Product(
                        "3",
                        "Baguette",
                        "Crispy long bread and taste salty",
                        25000,
                        "https://pbs.twimg.com/media/F6jwl46XQAATWJM.jpg",
                        20
                    ),
                    Product(
                        "4",
                        "Pen",
                        "Making smooth line is great with this",
                        5000,
                        "https://ae01.alicdn.com/kf/Sa24a7b5dd57d49a99aee27cf53343876r.jpg",
                        200
                    )
                )
                productDao.insertProducts(dummyProducts)
            }
        }
    }

    suspend fun updateProduct(product: Product) {
        withContext(Dispatchers.IO) {
            productDao.updateProduct(product)
        }
    }

    suspend fun clearCart() {
        withContext(Dispatchers.IO) {
            productDao.clearCart()
        }
    }
}
