package com.applemand.zmanim.core.settings

import androidx.datastore.core.CorruptionException
import androidx.datastore.core.Serializer
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 com.google.protobuf.InvalidProtocolBufferException
import java.io.InputStream
import java.io.OutputStream

/**
 * Default user settings — mirrors webapp src/lib/settingsContext.jsx defaults.
 */
internal val DefaultUserSettings: UserSettings = UserSettings.newBuilder()
    .setCurrentLocation(
        Location.newBuilder()
            .setNameEn("New York")
            .setNameHe("ניו יורק")
            .setLat(40.7128)
            .setLng(-74.0060)
            .setTzid("America/New_York")
            .setCountry("US")
            .build()
    )
    .setUiLanguage(Language.LANGUAGE_SYSTEM)
    .setTempUnit(TempUnit.TEMP_FAHRENHEIT)
    .setShowZmanim(true)
    .setShowZmanimSeconds(false)
    .putAllZmanimVisible(
        mapOf(
            "alotHaShachar" to true, "zmanTzitzit" to true, "sunrise" to true,
            "sofShmaGRA" to true, "sofShmaMA" to true, "sofTfila" to true,
            "midday" to true, "minchaGedolah" to true, "plagHaMincha" to true,
            "sunset" to true, "candleLighting" to true,
            "tzeitKochavim" to true, "rabbeinuTam" to true,
        )
    )
    .setCandleLightingMinutes(18)
    .setHavdalahMinutes(50)
    .setShowStatusBar(true)
    .setShowDafYomi(true)
    .setEnableNotifications(false)
    .setAutoSyncLocation(false)
    .setHolidayFilters(
        HolidayFilters.newBuilder()
            .setYomTov(true).setFast(true).setMinor(true).setRoshChodesh(true).build()
    )
    .setSyncZmanimDays(30)
    .setThemeMode(ThemeMode.THEME_SYSTEM)
    .setUseDynamicColor(true)
    .setFirstDaySunday(true)
    .setNotifWindows(
        NotificationWindows.newBuilder()
            .setDailySummaryEnabled(false)
            .setDailySummaryHour(8).setDailySummaryMinute(0)
            .setCandleLightingEnabled(false).setCandleLightingLeadMin(15)
            .setHavdalahEnabled(false)
            .setFastStartEnabled(false).setFastEndEnabled(false)
            .setRoshChodeshEnabled(false)
            .setDafYomiEnabled(false).setDafYomiHour(6).setDafYomiMinute(0)
            .build()
    )
    .setInIsrael(false)
    .setTwelveHourClock(true)
    .setUseElevationForZmanim(false)
    .setSchemaVersion(1L)
    .build()

object UserSettingsSerializer : Serializer<UserSettings> {
    override val defaultValue: UserSettings = DefaultUserSettings

    override suspend fun readFrom(input: InputStream): UserSettings = try {
        UserSettings.parseFrom(input)
    } catch (e: InvalidProtocolBufferException) {
        throw CorruptionException("UserSettings proto corrupted", e)
    }

    override suspend fun writeTo(t: UserSettings, output: OutputStream) {
        t.writeTo(output)
    }
}
