package com.example.myapplication.database.room.viewmodel.bike

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.example.myapplication.database.room.model.bike.Bike
import com.example.myapplication.database.room.repository.online.BikeRepository
import com.example.myapplication.database.room.viewmodel.LoadingViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow

class BikeListViewModel(
    private val bikeRepository: BikeRepository
) : LoadingViewModel() {
    var bikePagingFlowState by mutableStateOf(BikePagingFlowState())
    private set

    fun refresh() {
        runInScope(actionSuccess = {
            val pagingSource = bikeRepository.getAllBikes()
            bikePagingFlowState = BikePagingFlowState(pagingSource.cachedIn(viewModelScope))
        })
    }
    suspend fun deleteBike(bike: Bike) {
        runInScope(actionSuccess = {
            bikeRepository.deleteBike(bike)
        })
    }
}

data class BikePagingFlowState(
    val flow: Flow<PagingData<Bike>> = emptyFlow(),
)