package com.zyzf.coffeepreorder.database.repository

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.PagingSource
import com.zyzf.coffeepreorder.database.AppContainer
import com.zyzf.coffeepreorder.database.dao.UserDao
import com.zyzf.coffeepreorder.database.model.User
import kotlinx.coroutines.flow.Flow

class OfflineUserRepository(private val userDao: UserDao) : UserRepository {
    override fun getAll(): Flow<PagingData<User>> = Pager(
        config = PagingConfig(
            pageSize = AppContainer.LIMIT,
            enablePlaceholders = false
        ),
        pagingSourceFactory = userDao::getAll
    ).flow
    fun getAllUserPagingSource(): PagingSource<Int, User> = userDao.getAll()
    override suspend fun getByUid(uid: Int): User? = userDao.getByUid(uid)
    override suspend fun tryLogin(login: String, password: String): User? = userDao.tryLogin(login, password)
    override suspend fun insert(user: User): Long = userDao.insert(user)
    fun insertUsers(users: List<User>) =
        userDao.insert(*users.toTypedArray())
    override suspend fun update(user: User): Int? = userDao.update(user)
    override suspend fun delete(user: User) = userDao.delete(user)
    suspend fun clearUsers() = userDao.deleteAll()
}