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")
    }

    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 } }
}
