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

import com.github.nullptroma.wallenc.domain.errors.WallencException
import com.github.nullptroma.wallenc.ui.R
import com.github.nullptroma.wallenc.usecases.AddStorageToSyncGroupResult
import com.github.nullptroma.wallenc.vault.contract.VaultLinkFailure

fun WallencException.toUserNotification(): UserNotification.TextRes = when (this) {
    is WallencException.Feature.StorageNotFound ->
        UserNotification.TextRes(R.string.error_storage_not_found)
    is WallencException.Feature.NeedsDecryptedView ->
        UserNotification.TextRes(R.string.error_storage_locked_view)
    is WallencException.Feature.SecretNotFound ->
        UserNotification.TextRes(R.string.error_secret_not_found)
    is WallencException.Feature.StorageNotWritable ->
        UserNotification.TextRes(R.string.error_storage_not_writable)

    is WallencException.Storage.NotAvailable,
    is WallencException.Storage.NotWritable,
    ->
        UserNotification.TextRes(R.string.error_storage_not_writable)
    is WallencException.Storage.FileNotFound ->
        UserNotification.TextRes(R.string.error_file_not_found)
    is WallencException.Storage.IncorrectKey ->
        UserNotification.TextRes(R.string.error_incorrect_password)
    is WallencException.Storage.NotEncrypted ->
        UserNotification.TextRes(R.string.error_storage_not_encrypted)
    is WallencException.Storage.EncInfoMissing ->
        UserNotification.TextRes(R.string.error_enc_info_missing)
    is WallencException.Storage.DeleteRootForbidden ->
        UserNotification.TextRes(R.string.error_delete_root_forbidden)
    is WallencException.Storage.NotAFile ->
        UserNotification.TextRes(R.string.error_not_a_file)
    is WallencException.Storage.NotADirectory ->
        UserNotification.TextRes(R.string.error_not_a_directory)
    is WallencException.Storage.PathIsFile ->
        UserNotification.TextRes(R.string.error_path_is_file)
    is WallencException.Storage.CannotWriteOverDirectory ->
        UserNotification.TextRes(R.string.error_cannot_write_over_directory)
    is WallencException.Storage.UnexpectedState ->
        UserNotification.TextRes(R.string.error_unexpected_state)
    is WallencException.Storage.IoFailed ->
        UserNotification.TextRes(R.string.error_network)

    is WallencException.Auth.Failed,
    is WallencException.Auth.TokenMissing,
    ->
        UserNotification.TextRes(R.string.vault_link_error_auth)
    is WallencException.Network.ResourceLocked ->
        UserNotification.TextRes(R.string.error_disk_resource_locked)
    is WallencException.Network.OperationFailed,
    is WallencException.Network.OperationTimedOut,
    is WallencException.Network.HttpFailed,
    is WallencException.Network.IoFailed,
    ->
        UserNotification.TextRes(R.string.error_network)

    is WallencException.Unknown ->
        UserNotification.TextRes(R.string.error_unknown)
}

fun VaultLinkFailure.toUserNotification(): UserNotification.TextRes = when (this) {
    VaultLinkFailure.UnsupportedBrand ->
        UserNotification.TextRes(R.string.vault_link_error_unsupported_brand)
    VaultLinkFailure.NotRegistered ->
        UserNotification.TextRes(R.string.vault_link_error_not_registered)
    VaultLinkFailure.AuthError ->
        UserNotification.TextRes(R.string.vault_link_error_auth)
    VaultLinkFailure.Unknown ->
        UserNotification.TextRes(R.string.vault_link_error_unknown)
}

fun AddStorageToSyncGroupResult.toUserNotification(groupId: String): UserNotification.TextRes = when (this) {
    AddStorageToSyncGroupResult.Added ->
        UserNotification.TextRes(R.string.sync_msg_storage_added, listOf(groupId))
    AddStorageToSyncGroupResult.GroupNotFound ->
        UserNotification.TextRes(R.string.sync_error_group_not_found)
    AddStorageToSyncGroupResult.AlreadyInGroup ->
        UserNotification.TextRes(R.string.sync_msg_storage_already_added)
    AddStorageToSyncGroupResult.EncryptedStorageNotAllowed ->
        UserNotification.TextRes(R.string.sync_msg_only_plain_storage_allowed)
    AddStorageToSyncGroupResult.MissingEncryptionSecret ->
        UserNotification.TextRes(R.string.sync_msg_storage_encryption_key_required)
    AddStorageToSyncGroupResult.IncompatibleEncryption ->
        UserNotification.TextRes(R.string.sync_msg_storage_incompatible_encryption)
}
