package app.closer.data.local

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 app.closer.core.notifications.QuietHours
import app.closer.domain.repository.AppSettings
import app.closer.domain.repository.SettingsRepository
import app.closer.domain.repository.ThemeMode
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class SettingsDataStore @Inject constructor(
    private val dataStore: DataStore<Preferences>
) : SettingsRepository {

    private val DAILY_REMINDER = booleanPreferencesKey("daily_reminder")
    private val PARTNER_ANSWERED = booleanPreferencesKey("partner_answered")
    private val STREAK_REMINDER = booleanPreferencesKey("streak_reminder")
    private val QUIET_HOURS = booleanPreferencesKey("quiet_hours")
    private val QUIET_HOURS_START_HOUR = intPreferencesKey("quiet_hours_start_hour")
    private val QUIET_HOURS_START_MINUTE = intPreferencesKey("quiet_hours_start_minute")
    private val QUIET_HOURS_END_HOUR = intPreferencesKey("quiet_hours_end_hour")
    private val QUIET_HOURS_END_MINUTE = intPreferencesKey("quiet_hours_end_minute")
    private val ONBOARDING_COMPLETE = booleanPreferencesKey("onboarding_complete")
    private val THEME_MODE = stringPreferencesKey("theme_mode")

    override val settings: Flow<AppSettings> = dataStore.data.map { prefs ->
        AppSettings(
            dailyReminderEnabled = prefs[DAILY_REMINDER] ?: true,
            partnerAnsweredEnabled = prefs[PARTNER_ANSWERED] ?: true,
            streakReminderEnabled = prefs[STREAK_REMINDER] ?: false,
            quietHoursEnabled = prefs[QUIET_HOURS] ?: false,
            quietHours = QuietHours(
                enabled = prefs[QUIET_HOURS] ?: false,
                startHour = prefs[QUIET_HOURS_START_HOUR] ?: 22,
                startMinute = prefs[QUIET_HOURS_START_MINUTE] ?: 0,
                endHour = prefs[QUIET_HOURS_END_HOUR] ?: 8,
                endMinute = prefs[QUIET_HOURS_END_MINUTE] ?: 0
            ),
            onboardingComplete = prefs[ONBOARDING_COMPLETE] ?: false,
            themeMode = ThemeMode.fromStorageValue(prefs[THEME_MODE])
        )
    }

    override suspend fun setDailyReminder(enabled: Boolean) =
        dataStore.edit { it[DAILY_REMINDER] = enabled }.let {}

    override suspend fun setPartnerAnswered(enabled: Boolean) =
        dataStore.edit { it[PARTNER_ANSWERED] = enabled }.let {}

    override suspend fun setStreakReminder(enabled: Boolean) =
        dataStore.edit { it[STREAK_REMINDER] = enabled }.let {}

    override suspend fun setQuietHours(enabled: Boolean) =
        dataStore.edit { it[QUIET_HOURS] = enabled }.let {}

    override suspend fun setQuietHours(quietHours: QuietHours) =
        dataStore.edit {
            it[QUIET_HOURS] = quietHours.enabled
            it[QUIET_HOURS_START_HOUR] = quietHours.startHour
            it[QUIET_HOURS_START_MINUTE] = quietHours.startMinute
            it[QUIET_HOURS_END_HOUR] = quietHours.endHour
            it[QUIET_HOURS_END_MINUTE] = quietHours.endMinute
        }.let {}

    override suspend fun setOnboardingComplete(complete: Boolean) =
        dataStore.edit { it[ONBOARDING_COMPLETE] = complete }.let {}

    override suspend fun setThemeMode(mode: ThemeMode) =
        dataStore.edit { it[THEME_MODE] = mode.name }.let {}
}
