package swiss.qpq.gajumobile.security

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

object SecurityManager {
    private val _isLocked = MutableStateFlow(true)
    val isLocked = _isLocked.asStateFlow()

    private var lastBackgroundTime: Long = 0
    private const val LOCK_TIMEOUT_MS = 2 * 60 * 1000L // 2 minutes

    fun lock() {
        _isLocked.value = true
    }

    fun unlock() {
        _isLocked.value = false
    }

    fun onAppBackgrounded() {
        lastBackgroundTime = System.currentTimeMillis()
    }

    fun onAppForegrounded() {
        if (lastBackgroundTime != 0L && System.currentTimeMillis() - lastBackgroundTime > LOCK_TIMEOUT_MS) {
            lock()
        }
        // Reset background time after checking
        lastBackgroundTime = 0
    }
}
