package com.mindmachine.mvp.data

import android.content.Context
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.mindmachine.mvp.domain.AppSettings
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

private val Context.dataStore by preferencesDataStore("mindmachine_settings")

class SettingsRepository(private val context: Context) {
    private object Keys {
        val safetyAcknowledged = booleanPreferencesKey("safety_ack")
        val safetyVersion = intPreferencesKey("safety_version")
        val countdownPref = stringPreferencesKey("countdown_pref")
        val countdownSeconds = intPreferencesKey("countdown_seconds")
        val forceAudioWithoutHeadphones = booleanPreferencesKey("force_audio_without_headphones")
        val showImmersiveProgressBar = booleanPreferencesKey("show_immersive_progress_bar")
        val immersiveBrightnessPercent = intPreferencesKey("immersive_brightness_percent")
        val lastPresetId = stringPreferencesKey("last_preset")
        val noAds = booleanPreferencesKey("no_ads")
    }

    val settings: Flow<AppSettings> = context.dataStore.data.map { p ->
        AppSettings(
            safetyAcknowledged = p[Keys.safetyAcknowledged] ?: false,
            safetyAcknowledgedVersion = p[Keys.safetyVersion] ?: 0,
            countdownSeconds = p[Keys.countdownSeconds]
                ?: legacyCountdownStringToSeconds(p[Keys.countdownPref])
                ?: 5,
            forceAudioWithoutHeadphones = p[Keys.forceAudioWithoutHeadphones] ?: false,
            showImmersiveProgressBar = p[Keys.showImmersiveProgressBar] ?: true,
            immersiveBrightnessPercent = (p[Keys.immersiveBrightnessPercent] ?: 100).coerceIn(5, 100),
            lastPresetId = p[Keys.lastPresetId],
            noAds = p[Keys.noAds] ?: false,
        )
    }

    suspend fun acknowledgeSafety(version: Int) {
        context.dataStore.edit {
            it[Keys.safetyAcknowledged] = true
            it[Keys.safetyVersion] = version
        }
    }

    suspend fun updateCountdownSeconds(value: Int) = context.dataStore.edit {
        it[Keys.countdownSeconds] = value.coerceIn(0, 10)
    }

    suspend fun updateForceAudioWithoutHeadphones(value: Boolean) = context.dataStore.edit {
        it[Keys.forceAudioWithoutHeadphones] = value
    }

    suspend fun updateShowImmersiveProgressBar(value: Boolean) = context.dataStore.edit {
        it[Keys.showImmersiveProgressBar] = value
    }

    suspend fun updateImmersiveBrightnessPercent(value: Int) = context.dataStore.edit {
        it[Keys.immersiveBrightnessPercent] = value.coerceIn(5, 100)
    }

    suspend fun updateLastPreset(id: String) = context.dataStore.edit { it[Keys.lastPresetId] = id }

    suspend fun updateNoAds(value: Boolean) = context.dataStore.edit { it[Keys.noAds] = value }

    private fun legacyCountdownStringToSeconds(raw: String?): Int? = when (raw) {
        "OFF" -> 0
        "FIVE" -> 5
        "TEN" -> 10
        else -> null
    }
}
