package com.radiola.data.repository

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
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.core.stringSetPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.radiola.domain.repository.SettingsRepository
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")

class SettingsRepositoryImpl @Inject constructor(
    @ApplicationContext private val context: Context
) : SettingsRepository {

    private val dataStore = context.dataStore

    companion object {
        private val LAST_STATION_ID = intPreferencesKey("last_station_id")
        private val SLEEP_TIMER = intPreferencesKey("sleep_timer_minutes")
        private val ENABLED_SERVICES = stringSetPreferencesKey("enabled_deeplink_services")
        private val EQUALIZER_PRESET = stringPreferencesKey("equalizer_preset")
        private val RECORDING_ENABLED = booleanPreferencesKey("recording_enabled")
        private val PREFERRED_BITRATE = intPreferencesKey("preferred_bitrate")
        private val COUNTRY_CODE = stringPreferencesKey("country_code")
        private val VISUALIZER_STYLE = stringPreferencesKey("visualizer_style")
        private val THEME_PALETTE = stringPreferencesKey("theme_palette")
        private val EQ_ENABLED = booleanPreferencesKey("eq_enabled")
        private val EQ_PRESET = intPreferencesKey("eq_preset")
        private val EQ_BANDS = stringPreferencesKey("eq_bands")
        private val EQ_BASS = intPreferencesKey("eq_bass")
        private val EQ_VIRTUALIZER = intPreferencesKey("eq_virtualizer")
        private val EQ_LOUDNESS = intPreferencesKey("eq_loudness")
    }

    override fun getLastStationId(): Flow<Int?> = dataStore.data.map { it[LAST_STATION_ID] }
    override suspend fun setLastStationId(id: Int) { dataStore.edit { it[LAST_STATION_ID] = id } }

    override fun getSleepTimerMinutes(): Flow<Int> = dataStore.data.map { it[SLEEP_TIMER] ?: 30 }
    override suspend fun setSleepTimerMinutes(minutes: Int) { dataStore.edit { it[SLEEP_TIMER] = minutes } }

    override fun getEnabledDeeplinkServices(): Flow<Set<String>> = dataStore.data.map {
        it[ENABLED_SERVICES] ?: setOf("yandex", "vk", "spotify", "apple", "youtube")
    }
    override suspend fun setEnabledDeeplinkServices(serviceIds: Set<String>) {
        dataStore.edit { it[ENABLED_SERVICES] = serviceIds }
    }

    override fun getEqualizerPreset(): Flow<String> = dataStore.data.map { it[EQUALIZER_PRESET] ?: "Flat" }
    override suspend fun setEqualizerPreset(preset: String) { dataStore.edit { it[EQUALIZER_PRESET] = preset } }

    override fun isRecordingEnabled(): Flow<Boolean> = dataStore.data.map { it[RECORDING_ENABLED] ?: false }
    override suspend fun setRecordingEnabled(enabled: Boolean) { dataStore.edit { it[RECORDING_ENABLED] = enabled } }

    override fun getPreferredBitrate(): Flow<Int> = dataStore.data.map { it[PREFERRED_BITRATE] ?: 0 }
    override suspend fun setPreferredBitrate(bitrate: Int) { dataStore.edit { it[PREFERRED_BITRATE] = bitrate } }

    override fun getCountryCode(): Flow<String?> = dataStore.data.map { it[COUNTRY_CODE] }
    override suspend fun setCountryCode(code: String) { dataStore.edit { it[COUNTRY_CODE] = code } }

    override fun getVisualizerStyle(): Flow<String> = dataStore.data.map { it[VISUALIZER_STYLE] ?: "bars_center" }
    override suspend fun setVisualizerStyle(style: String) { dataStore.edit { it[VISUALIZER_STYLE] = style } }

    override fun getThemePalette(): Flow<String> = dataStore.data.map { it[THEME_PALETTE] ?: "forest" }
    override suspend fun setThemePalette(id: String) { dataStore.edit { it[THEME_PALETTE] = id } }

    override fun getEqEnabled(): Flow<Boolean> = dataStore.data.map { it[EQ_ENABLED] ?: false }
    override suspend fun setEqEnabled(enabled: Boolean) { dataStore.edit { it[EQ_ENABLED] = enabled } }

    override fun getEqPreset(): Flow<Int> = dataStore.data.map { it[EQ_PRESET] ?: -1 }
    override suspend fun setEqPreset(index: Int) { dataStore.edit { it[EQ_PRESET] = index } }

    override fun getEqBands(): Flow<String> = dataStore.data.map { it[EQ_BANDS] ?: "" }
    override suspend fun setEqBands(csv: String) { dataStore.edit { it[EQ_BANDS] = csv } }

    override fun getEqBass(): Flow<Int> = dataStore.data.map { it[EQ_BASS] ?: 0 }
    override suspend fun setEqBass(value: Int) { dataStore.edit { it[EQ_BASS] = value } }

    override fun getEqVirtualizer(): Flow<Int> = dataStore.data.map { it[EQ_VIRTUALIZER] ?: 0 }
    override suspend fun setEqVirtualizer(value: Int) { dataStore.edit { it[EQ_VIRTUALIZER] = value } }

    override fun getEqLoudness(): Flow<Int> = dataStore.data.map { it[EQ_LOUDNESS] ?: 0 }
    override suspend fun setEqLoudness(value: Int) { dataStore.edit { it[EQ_LOUDNESS] = value } }
}
