package com.pixelized.desktop.lwa.ui.screen.admin

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.pixelized.desktop.lwa.repository.settings.SettingsRepository
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

class AdminViewModel(
    private val settingsRepository: SettingsRepository,
): ViewModel() {

    val isGameMaster = settingsRepository.settingsFlow()
        .map { it.isGameMaster ?: false }
        .stateIn(
            scope = viewModelScope,
            started = SharingStarted.Eagerly,
            initialValue = false,
        )

    fun onGameMaster(value: Boolean) {
        val settings = settingsRepository.settings()
        settingsRepository.update(
            settings = settings.copy(
                isGameMaster = value,
            )
        )
    }
}