package com.tangem.feature.wallet.presentation.wallet.analytics.utils

import com.tangem.core.analytics.api.AnalyticsEventHandler
import com.tangem.core.analytics.models.AnalyticsEvent
import com.tangem.domain.models.wallet.UserWallet
import com.tangem.domain.models.wallet.isImported
import com.tangem.domain.models.wallet.isLocked
import com.tangem.feature.wallet.presentation.wallet.analytics.WalletScreenAnalyticsEvent
import com.tangem.feature.wallet.presentation.wallet.utils.ScreenLifecycleProvider
import javax.inject.Inject

internal class SelectedWalletAnalyticsSender @Inject constructor(
    private val analyticsEventHandler: AnalyticsEventHandler,
    private val screenLifecycleProvider: ScreenLifecycleProvider,
) {

    fun send(userWallet: UserWallet) {
        if (screenLifecycleProvider.isBackgroundState.value) return

        val event = getEvent(userWallet)

        if (event != null) {
            analyticsEventHandler.send(event)
        }
    }

    /**

     * that cannot be processed in [WalletWarningsAnalyticsSender].
     * */
    private fun getEvent(userWallet: UserWallet): AnalyticsEvent? = when {
        userWallet.isLocked -> WalletScreenAnalyticsEvent.MainScreen.WalletUnlock()

        else -> WalletScreenAnalyticsEvent.MainScreen.WalletSelected(userWallet.isImported())
    }
}