package ru.forbion.f7cloud.feature.calendar

import android.content.Context

class CalendarPrefsStore(context: Context) {
    private val prefs = context.applicationContext.getSharedPreferences(PREFS, Context.MODE_PRIVATE)

    fun getViewMode(accountKey: String): CalendarViewMode {
        val raw = prefs.getString(keyViewMode(accountKey), CalendarViewMode.MONTH.name)
        return runCatching { CalendarViewMode.valueOf(raw ?: CalendarViewMode.MONTH.name) }
            .getOrDefault(CalendarViewMode.MONTH)
    }

    fun setViewMode(accountKey: String, mode: CalendarViewMode) {
        prefs.edit().putString(keyViewMode(accountKey), mode.name).apply()
    }

    fun getVisibleCalendarHrefs(accountKey: String): Set<String> {
        val raw = prefs.getStringSet(keyVisibleCalendars(accountKey), null)
        return raw ?: emptySet()
    }

    fun setVisibleCalendarHrefs(accountKey: String, hrefs: Set<String>) {
        prefs.edit().putStringSet(keyVisibleCalendars(accountKey), hrefs).apply()
    }

    fun getCreateCalendarHref(accountKey: String): String? =
        prefs.getString(keyCreateCalendar(accountKey), null)?.takeIf { it.isNotBlank() }

    fun setCreateCalendarHref(accountKey: String, href: String) {
        prefs.edit().putString(keyCreateCalendar(accountKey), href).apply()
    }

    fun getUserSettings(accountKey: String): CalendarUserSettings = CalendarUserSettings(
        timezone = prefs.getString(keySetting(accountKey, "timezone"), "automatic") ?: "automatic",
        showWeekends = prefs.getBoolean(keySetting(accountKey, "showWeekends"), true),
        showWeekNumbers = prefs.getBoolean(keySetting(accountKey, "showWeekNr"), false),
        defaultReminderMinutes = prefs.getInt(keySetting(accountKey, "defaultReminder"), 15),
        showTasks = prefs.getBoolean(keySetting(accountKey, "showTasks"), true),
        tasksSidebar = prefs.getBoolean(keySetting(accountKey, "tasksSidebar"), true),
    )

    fun setUserSettings(accountKey: String, settings: CalendarUserSettings) {
        prefs.edit()
            .putString(keySetting(accountKey, "timezone"), settings.timezone)
            .putBoolean(keySetting(accountKey, "showWeekends"), settings.showWeekends)
            .putBoolean(keySetting(accountKey, "showWeekNr"), settings.showWeekNumbers)
            .putInt(keySetting(accountKey, "defaultReminder"), settings.defaultReminderMinutes)
            .putBoolean(keySetting(accountKey, "showTasks"), settings.showTasks)
            .putBoolean(keySetting(accountKey, "tasksSidebar"), settings.tasksSidebar)
            .apply()
    }

    private fun keySetting(account: String, key: String) = "setting_${key}_$account"

    companion object {
        private const val PREFS = "f7_calendar_prefs"

        private fun keyViewMode(account: String) = "view_mode::$account"
        private fun keyVisibleCalendars(account: String) = "visible_cals::$account"
        private fun keyCreateCalendar(account: String) = "create_cal::$account"
    }
}
