package com.swoosh.microblog.ui.theme

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

/**
 * App-level ViewModel that manages theme mode state.
 * Should be scoped to the Activity so all screens share the same instance.
 */
class ThemeViewModel(application: Application) : AndroidViewModel(application) {

    private val themePreferences = ThemePreferences(application)

    private val _themeMode = MutableStateFlow(themePreferences.themeMode)
    val themeMode: StateFlow<ThemeMode> = _themeMode.asStateFlow()

    fun setThemeMode(mode: ThemeMode) {
        themePreferences.themeMode = mode
        _themeMode.value = mode
    }

    /**
     * Cycles through modes: SYSTEM -> LIGHT -> DARK -> SYSTEM.
     */
    fun cycleThemeMode() {
        setThemeMode(_themeMode.value.next())
    }
}
