package eu.darken.sdmse.main.ui.onboarding.privacy

import androidx.lifecycle.SavedStateHandle
import dagger.hilt.android.lifecycle.HiltViewModel
import eu.darken.sdmse.common.SdmSeLinks
import eu.darken.sdmse.common.WebpageTool
import eu.darken.sdmse.common.coroutine.DispatcherProvider
import eu.darken.sdmse.common.datastore.value
import eu.darken.sdmse.common.debug.logging.log
import eu.darken.sdmse.common.debug.logging.logTag
import eu.darken.sdmse.common.uix.ViewModel4
import eu.darken.sdmse.common.updater.UpdateChecker
import eu.darken.sdmse.main.core.GeneralSettings
import eu.darken.sdmse.main.core.motd.MotdSettings
import eu.darken.sdmse.main.ui.navigation.OnboardingSetupRoute
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

@HiltViewModel
class OnboardingPrivacyViewModel @Inject constructor(
    @Suppress("unused") private val handle: SavedStateHandle,
    dispatcherProvider: DispatcherProvider,
    private val generalSettings: GeneralSettings,
    private val motdSettings: MotdSettings,
    private val webpageTool: WebpageTool,
    private val updateChecker: UpdateChecker,
) : ViewModel4(dispatcherProvider = dispatcherProvider) {

    val state: StateFlow<State> = combine(
        motdSettings.isMotdEnabled.flow,
        generalSettings.isUpdateCheckEnabled.flow,
        flow { emit(updateChecker.isCheckSupported()) },
    ) { isMotdEnabled, isUpdateCheckEnabled, isUpdateCheckSupported ->
        State(
            isMotdEnabled = isMotdEnabled,
            isUpdateCheckEnabled = isUpdateCheckEnabled,
            isUpdateCheckSupported = isUpdateCheckSupported,
        )
    }.safeStateIn(
        initialValue = State(),
        onError = { State() },
    )

    fun onContinue() {
        navTo(OnboardingSetupRoute)
    }

    fun goPrivacyPolicy() {
        log(TAG) { "goPrivacyPolicy()" }
        webpageTool.open(SdmSeLinks.PRIVACY_POLICY)
    }

    fun toggleMotd() {
        log(TAG) { "toggleMotd()" }
        // Use the suspend value() on vmScope instead of valueBlocking (runBlocking on the main thread).
        launch { motdSettings.isMotdEnabled.value(!motdSettings.isMotdEnabled.value()) }
    }

    fun toggleUpdateCheck() {
        log(TAG) { "toggleUpdateCheck()" }
        launch { generalSettings.isUpdateCheckEnabled.value(!generalSettings.isUpdateCheckEnabled.value()) }
    }

    data class State(
        val isMotdEnabled: Boolean = true,
        val isUpdateCheckEnabled: Boolean = true,
        val isUpdateCheckSupported: Boolean = false,
    )

    companion object {
        private val TAG = logTag("Onboarding", "Privacy", "ViewModel")
    }
}
