package com.seven.ikar.kotlin.feature.settings

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.seven.ikar.kotlin.app.AppContainer
import com.seven.ikar.kotlin.core.settings.ChatSettingsSnapshot
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

data class ChatSettingsUiState(
    val snapshot: ChatSettingsSnapshot = ChatSettingsSnapshot()
)

class ChatSettingsViewModel(private val container: AppContainer) : ViewModel() {
    private val _uiState = MutableStateFlow(ChatSettingsUiState(snapshot = container.chatSettings.loadSnapshot()))
    val uiState: StateFlow<ChatSettingsUiState> = _uiState.asStateFlow()

    init {
        viewModelScope.launch {
            container.chatSettings.snapshot.collect { snapshot ->
                _uiState.value = ChatSettingsUiState(snapshot = snapshot)
            }
        }
    }

    fun updateSnapshot(transform: (ChatSettingsSnapshot) -> ChatSettingsSnapshot) {
        container.chatSettings.update(transform)
    }

    fun reset() {
        container.chatSettings.reset()
    }
}
