package com.applemand.zmanim.core.settings

import androidx.datastore.core.DataStore
import com.applemand.zmanim.core.settings.proto.HolidayFilters
import com.applemand.zmanim.core.settings.proto.Language
import com.applemand.zmanim.core.settings.proto.Location
import com.applemand.zmanim.core.settings.proto.NotificationWindows
import com.applemand.zmanim.core.settings.proto.TempUnit
import com.applemand.zmanim.core.settings.proto.ThemeMode
import com.applemand.zmanim.core.settings.proto.UserSettings
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class SettingsRepository @Inject constructor(
    private val dataStore: DataStore<UserSettings>,
) {
    val settings: Flow<UserSettings> = dataStore.data

    suspend fun setLocation(location: Location) = dataStore.updateData {
        it.toBuilder().setCurrentLocation(location).build()
    }

    suspend fun setUiLanguage(lang: Language) = dataStore.updateData {
        it.toBuilder().setUiLanguage(lang).build()
    }

    suspend fun setTempUnit(unit: TempUnit) = dataStore.updateData {
        it.toBuilder().setTempUnit(unit).build()
    }

    suspend fun setThemeMode(mode: ThemeMode) = dataStore.updateData {
        it.toBuilder().setThemeMode(mode).build()
    }

    suspend fun setUseDynamicColor(on: Boolean) = dataStore.updateData {
        it.toBuilder().setUseDynamicColor(on).build()
    }

    suspend fun setShowZmanim(on: Boolean) = dataStore.updateData {
        it.toBuilder().setShowZmanim(on).build()
    }

    suspend fun setShowZmanimSeconds(on: Boolean) = dataStore.updateData {
        it.toBuilder().setShowZmanimSeconds(on).build()
    }

    suspend fun setZmanVisible(key: String, visible: Boolean) = dataStore.updateData {
        it.toBuilder().putZmanimVisible(key, visible).build()
    }

    suspend fun setCandleLightingMinutes(minutes: Int) = dataStore.updateData {
        it.toBuilder().setCandleLightingMinutes(minutes.coerceIn(1, 60)).build()
    }

    suspend fun setHavdalahMinutes(minutes: Int) = dataStore.updateData {
        it.toBuilder().setHavdalahMinutes(minutes.coerceIn(30, 90)).build()
    }

    suspend fun setShowStatusBar(on: Boolean) = dataStore.updateData {
        it.toBuilder().setShowStatusBar(on).build()
    }

    suspend fun setShowDafYomi(on: Boolean) = dataStore.updateData {
        it.toBuilder().setShowDafYomi(on).build()
    }

    suspend fun setEnableNotifications(on: Boolean) = dataStore.updateData {
        it.toBuilder().setEnableNotifications(on).build()
    }

    suspend fun setAutoSyncLocation(on: Boolean) = dataStore.updateData {
        it.toBuilder().setAutoSyncLocation(on).build()
    }

    suspend fun setHolidayFilters(filters: HolidayFilters) = dataStore.updateData {
        it.toBuilder().setHolidayFilters(filters).build()
    }

    suspend fun setSyncZmanimDays(days: Int) = dataStore.updateData {
        it.toBuilder().setSyncZmanimDays(days.coerceIn(1, 365)).build()
    }

    suspend fun setTwelveHourClock(on: Boolean) = dataStore.updateData {
        it.toBuilder().setTwelveHourClock(on).build()
    }

    suspend fun setInIsrael(on: Boolean) = dataStore.updateData {
        it.toBuilder().setInIsrael(on).build()
    }

    suspend fun setUseElevationForZmanim(on: Boolean) = dataStore.updateData {
        it.toBuilder().setUseElevationForZmanim(on).build()
    }

    suspend fun setNotifWindows(w: NotificationWindows) = dataStore.updateData {
        it.toBuilder().setNotifWindows(w).build()
    }
}
