package ch.threema.app.services

import android.content.Context
import android.net.Uri
import androidx.annotation.StringRes
import androidx.core.app.NotificationCompat
import androidx.core.net.toUri
import ch.threema.app.R
import ch.threema.app.stores.PreferenceStore
import ch.threema.data.datatypes.ConversationIdObfuscated

class NotificationPreferenceServiceImpl(
    private val appContext: Context,
    private val preferenceStore: PreferenceStore,
) : NotificationPreferenceService {
    override fun isMasterKeyNewMessageNotifications(): Boolean =
        preferenceStore.getBoolean(getKeyName(R.string.preferences__masterkey_notification_newmsg))

    override fun setWizardRunning(running: Boolean) =
        preferenceStore.save(getKeyName(R.string.preferences__wizard_running), running)

    override fun getWizardRunning(): Boolean =
        preferenceStore.getBoolean(getKeyName(R.string.preferences__wizard_running))

    override fun getLegacyNotificationSound(): Uri? =
        preferenceStore.getRingtoneUri(R.string.preferences__notification_sound)

    override fun setLegacyNotificationSound(uri: Uri?) {
        preferenceStore.save(getKeyName(R.string.preferences__notification_sound), uri?.toString())
    }

    override fun getLegacyGroupNotificationSound(): Uri? =
        preferenceStore.getRingtoneUri(R.string.preferences__group_notification_sound)

    override fun setLegacyGroupNotificationSound(uri: Uri?) {
        preferenceStore.save(getKeyName(R.string.preferences__group_notification_sound), uri?.toString())
    }

    override fun getLegacyVoipCallRingtone(): Uri? =
        preferenceStore.getRingtoneUri(R.string.preferences__voip_ringtone)

    override fun setLegacyVoipCallRingtone(uri: Uri?) {
        preferenceStore.save(getKeyName(R.string.preferences__voip_ringtone), uri?.toString())
    }

    override fun getLegacyGroupCallRingtone(): Uri? =
        preferenceStore.getRingtoneUri(R.string.preferences__group_calls_ringtone)

    override fun isLegacyNotificationVibrate(): Boolean =
        preferenceStore.getBoolean(getKeyName(R.string.preferences__vibrate))

    override fun isLegacyGroupVibrate(): Boolean =
        preferenceStore.getBoolean(getKeyName(R.string.preferences__group_vibrate))

    override fun isLegacyVoipCallVibrate(): Boolean =
        preferenceStore.getBoolean(getKeyName(R.string.preferences__voip_vibration))

    override fun isLegacyGroupCallVibrate(): Boolean =
        preferenceStore.getBoolean(getKeyName(R.string.preferences__group_calls_vibration))

    override fun isLegacyNotificationLightEnabled(): Boolean =
        preferenceStore.getBoolean(getKeyName(R.string.preferences__notification_light_single), true)

    override fun isLegacyGroupNotificationLightEnabled(): Boolean =
        preferenceStore.getBoolean(getKeyName(R.string.preferences__notification_light_group), true)

    override fun isShowMessagePreview(): Boolean =
        preferenceStore.getBoolean(getKeyName(R.string.preferences__notification_preview))

    override fun getLegacyNotificationPriority(): Int =
        preferenceStore.getString(getKeyName(R.string.preferences__notification_priority))
            ?.toIntOrNull()
            ?: NotificationCompat.PRIORITY_HIGH

    override fun setLegacyNotificationPriority(value: Int) {
        preferenceStore.save(
            getKeyName(R.string.preferences__notification_priority),
            value.toString(),
        )
    }

    override fun getLegacyRingtones(): Map<ConversationIdObfuscated, String?> =
        preferenceStore
            .getMap(getKeyName(R.string.preferences__individual_ringtones))
            .mapKeys { mapEntry ->
                ConversationIdObfuscated(mapEntry.key)
            }

    override fun setLegacyRingtones(ringtones: Map<ConversationIdObfuscated, String?>) {
        preferenceStore.save(
            getKeyName(R.string.preferences__individual_ringtones),
            ringtones.mapKeys { (conversationIdObfuscated, _) ->
                conversationIdObfuscated.value
            },
        )
    }

    override fun getDisableSmartReplies(): Boolean =
        preferenceStore.getBoolean(getKeyName(R.string.preferences__disable_smart_replies), false)

    private fun getKeyName(@StringRes resourceId: Int): String =
        appContext.getString(resourceId)

    private fun PreferenceStore.getRingtoneUri(@StringRes ringtoneKey: Int): Uri? =
        getString(getKeyName(ringtoneKey))
            .takeUnless { it.isNullOrBlank() }
            ?.takeUnless { it == ServicesConstants.PREFERENCES_NULL }
            ?.toUri()
}
