# MainActivityViewModel.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2023 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */
    package dk.nsp.mingraviditet.ui.main

    import android.app.KeyguardManager
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import dagger.hilt.android.lifecycle.HiltViewModel
    import dk.nsp.mingraviditet.core.data.domain.settings.AppPreferences
    import dk.nsp.mingraviditet.core.data.repository.UserPreferencesRepository
    import dk.nsp.mingraviditet.core.data.util.userAcceptedAnonymousAgreement
    import dk.nsp.mingraviditet.core.data.util.userAcceptedDataAgreement
    import dk.nsp.mingraviditet.extension.toMainUIState
    import dk.nsp.mingraviditet.ui.main.navigation.TopLevelDestination
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.flow.MutableStateFlow
    import kotlinx.coroutines.flow.asStateFlow
    import kotlinx.coroutines.flow.first
    import kotlinx.coroutines.launch
    import javax.inject.Inject

    @HiltViewModel
    class MainActivityViewModel @Inject constructor(
        val preferencesRepository: UserPreferencesRepository,
        val keyguardManager: KeyguardManager,
    ) : ViewModel() {

        private val _startDestination = MutableStateFlow<MainActivityUiState?>(null)
        val startDestination = _startDestination.asStateFlow()

        private val _navDestinations = MutableStateFlow(listOf<TopLevelDestination>())
        val navDestinations = _navDestinations.asStateFlow()

        init {
            viewModelScope.launch(Dispatchers.IO) {
                val preferences: AppPreferences = preferencesRepository.preferences.first()
                postStartDestination(preferences)
            }

            viewModelScope.launch(Dispatchers.IO) {
                preferencesRepository.preferences.collect { prefs ->
                    if (prefs.userAcceptedDataAgreement()) {
                        _navDestinations.value = getDestinations(anonymous = false)
                        return@collect
                    }
                    if (prefs.userAcceptedAnonymousAgreement()) {
                        _navDestinations.value = getDestinations(anonymous = true)
                        return@collect
                    }
                }
            }
        }

        private fun postStartDestination(preferences: AppPreferences) {
            _startDestination.value = preferences.toMainUIState(
                keyguardManager = keyguardManager,
                userPreferencesRepository = preferencesRepository,
                coroutineScope = viewModelScope,
            )
        }

        private fun getDestinations(anonymous: Boolean) = if (anonymous) {
            TopLevelDestination.entries.filter { it != TopLevelDestination.JOURNAL }
        } else {
            TopLevelDestination.entries
        }
    }