package com.zyzf.coffeepreorder.database.repository

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.zyzf.coffeepreorder.database.AppContainer
import com.zyzf.coffeepreorder.database.dao.CartDao
import com.zyzf.coffeepreorder.database.model.Cart
import com.zyzf.coffeepreorder.database.model.Coffee
import kotlinx.coroutines.flow.Flow

class OfflineCartRepository(private val cartDao: CartDao) : CartRepository {
    override fun getAllInCart(): Flow<PagingData<Coffee>> = Pager(
        config = PagingConfig(
            pageSize = AppContainer.LIMIT,
            enablePlaceholders = false
        ),
        pagingSourceFactory = cartDao::getAllInCart
    ).flow
    override fun getSumInCart(): Double = cartDao.getSumInCart()
    override suspend fun getAll(): Cart = cartDao.getAll()
    override suspend fun insert(cart: Cart) = cartDao.insert(cart)
    override suspend fun insertCoffee(coffeeId: Int, count: Int) = cartDao.insertCoffee(coffeeId, count)
    override suspend fun deleteCoffee(coffeeId: Int, count: Int) = cartDao.deleteCoffee(coffeeId, count)
    override fun getCountForCoffee(coffeeId: Int): Double = cartDao.getCountForCoffee(coffeeId)
    override suspend fun update(cart: Cart) = cartDao.update(cart)
    override suspend fun deleteAll() = cartDao.deleteAll()
}
