package com.example.myapplication.database.entities.composeui

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import com.example.myapplication.database.entities.model.BikeWithItems
import com.example.myapplication.database.entities.repository.BikeRepository

class BikeViewModel(
    savedStateHandle: SavedStateHandle, private val bikeRepository: BikeRepository
) : ViewModel() {
    private val bikeUid: Int = checkNotNull(savedStateHandle["id"])

    var bikeUiState by mutableStateOf(BikeUiState())
        private set

    suspend fun refreshState() {
        if (bikeUid > 0) {
            bikeUiState = BikeUiState(bikeRepository.getBike(bikeUid))
        }
    }

//    init {
//        viewModelScope.launch {
//            if (bikeUid > 0) {
//                bikeUiState = BikeUiState(bikeRepository.getBike(bikeUid))
//            }
//        }
//    }

//    val bikeUiState: mutableStateOf(BikeUiState()) = bikeRepository.getBike(
//    bikeUid
//    ).map
//    {
//        BikeUiState(it)
//    }.stateIn(
//    scope = viewModelScope,
//    started = SharingStarted.WhileSubscribed(stopTimeoutMillis = AppDataContainer.TIMEOUT),
//    initialValue = BikeUiState()
//    )
}

data class BikeUiState(val bikeWithItems: BikeWithItems? = null)