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.push.NotificationPreferenceCatalog
import com.seven.ikar.kotlin.core.push.NotificationRepeatIntervalChoice
import com.seven.ikar.kotlin.core.push.NotificationRepeatIntervalOption
import com.seven.ikar.kotlin.core.push.NotificationSettingsSnapshot
import com.seven.ikar.kotlin.core.push.NotificationSoundChoice
import com.seven.ikar.kotlin.core.push.NotificationSoundOption
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

data class NotificationSettingsUiState(
    val snapshot: NotificationSettingsSnapshot,
    val isSaving: Boolean = false,
    val errorMessage: String? = null,
    val infoMessage: String? = null
)

class NotificationSettingsViewModel(private val container: AppContainer) : ViewModel() {
    private val _uiState = MutableStateFlow(
        NotificationSettingsUiState(snapshot = container.pushSettings.loadSnapshot())
    )
    val uiState: StateFlow<NotificationSettingsUiState> = _uiState.asStateFlow()

    val messageSoundOptions: List<NotificationSoundChoice> = NotificationPreferenceCatalog.messageSoundChoices
    val callRingtoneOptions: List<NotificationSoundChoice> = NotificationPreferenceCatalog.callRingtoneChoices
    val repeatIntervalOptions: List<NotificationRepeatIntervalChoice> = NotificationPreferenceCatalog.repeatChoices

    fun updateSnapshot(transform: (NotificationSettingsSnapshot) -> NotificationSettingsSnapshot) {
        _uiState.value = _uiState.value.copy(snapshot = transform(_uiState.value.snapshot))
    }

    fun setMessageSound(value: NotificationSoundOption) = updateSnapshot { it.copy(messageSound = value) }

    fun setCallRingtone(value: NotificationSoundOption) = updateSnapshot { it.copy(callRingtone = value) }

    fun setRepeatInterval(value: NotificationRepeatIntervalOption) = updateSnapshot { it.copy(repeatInterval = value) }

    fun save() {
        viewModelScope.launch {
            val snapshot = _uiState.value.snapshot
            _uiState.value = _uiState.value.copy(isSaving = true, errorMessage = null, infoMessage = null)
            runCatching {
                container.pushSettings.saveSnapshot(snapshot)
                container.pushRegistrationManager.registerCurrentDeviceAsync()
            }.onSuccess {
                _uiState.value = _uiState.value.copy(isSaving = false, infoMessage = "Настройки уведомлений сохранены.")
            }.onFailure { error ->
                _uiState.value = _uiState.value.copy(isSaving = false, errorMessage = error.message)
            }
        }
    }

    fun reset() {
        container.pushSettings.resetNotificationPreferences()
        _uiState.value = NotificationSettingsUiState(snapshot = container.pushSettings.createDefaultSnapshot())
    }
}
