package com.github.nullptroma.wallenc.ui.resources

import com.github.nullptroma.wallenc.domain.tasks.StorageSyncTriggerReason
import com.github.nullptroma.wallenc.domain.tasks.TaskLogKey
import com.github.nullptroma.wallenc.ui.R

fun TaskLogKey.resolve(resolver: UiStringResolver): String = when (this) {
    is TaskLogKey.SyncStarted -> resolver(
        R.string.task_log_sync_started,
        resolver.resolveSyncTriggerReason(reason),
    )
    is TaskLogKey.SyncFinished -> resolver(
        R.string.task_log_sync_finished,
        resolver.resolveSyncTriggerReason(reason),
    )
    is TaskLogKey.SyncFailed -> {
        val notification = error.toUserNotification().resolve(resolver)
        resolver(
            R.string.task_log_sync_failed,
            resolver.resolveSyncTriggerReason(reason),
            notification,
        )
    }
}

fun UiStringResolver.storageSyncTaskTitle(reason: StorageSyncTriggerReason): String =
    this(R.string.task_title_storage_sync, resolveSyncTriggerReason(reason))

private fun UiStringResolver.resolveSyncTriggerReason(reason: StorageSyncTriggerReason): String =
    when (reason) {
        StorageSyncTriggerReason.Debounce -> this(R.string.task_sync_trigger_debounce)
        StorageSyncTriggerReason.SyncTab -> this(R.string.task_sync_trigger_sync_tab)
        StorageSyncTriggerReason.Background -> this(R.string.task_sync_trigger_background)
    }
