package com.tangem.features.send.send.analytics

import com.tangem.core.analytics.api.AnalyticsEventHandler
import com.tangem.core.analytics.models.AnalyticsParam
import com.tangem.core.analytics.models.Basic
import com.tangem.core.decompose.di.ModelScoped
import com.tangem.domain.account.status.usecase.GetAccountCurrencyByAddressUseCase
import com.tangem.domain.models.account.Account
import com.tangem.domain.models.account.derivationIndex
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.features.send.api.subcomponents.feeSelector.entity.FeeNonce
import com.tangem.features.send.api.subcomponents.feeSelector.entity.FeeSelectorUM
import com.tangem.features.send.api.subcomponents.destination.entity.DestinationTextFieldUM
import com.tangem.features.send.api.subcomponents.destination.entity.DestinationUM
import com.tangem.features.send.send.ui.state.SendUM
import javax.inject.Inject

@ModelScoped
internal class SendAnalyticHelper @Inject constructor(
    private val analyticsEventHandler: AnalyticsEventHandler,
    private val getAccountCurrencyByAddressUseCase: GetAccountCurrencyByAddressUseCase,
) {

    suspend fun sendSuccessAnalytics(
        cryptoCurrency: CryptoCurrency,
        sendUM: SendUM,
        feeToken: CryptoCurrency,
        account: Account?,
    ) {
        val destinationUM = sendUM.destinationUM as? DestinationUM.Content
        val feeSelectorUM = sendUM.feeSelectorUM as? FeeSelectorUM.Content ?: return
        val feeType = feeSelectorUM.toAnalyticType()
        val feeTokenSymbol = feeToken.symbol
        val feeAssetType = if (feeToken is CryptoCurrency.Coin) {
            AnalyticsParam.FeeAssetType.Coin
        } else {
            AnalyticsParam.FeeAssetType.Token
        }
        val fromDerivationIndex = account?.derivationIndex?.value
        val destination = destinationUM?.addressTextField?.actualAddress ?: return
        val destinationAccount = getAccountCurrencyByAddressUseCase(destination)
            .getOrNull()?.account
        val toDerivationIndex = destinationAccount?.derivationIndex?.value
        analyticsEventHandler.send(
            SendAnalyticEvents.TransactionScreenOpened(
                token = cryptoCurrency.symbol,
                feeType = feeType,
                blockchain = cryptoCurrency.network.name,
                isNonceNotEmpty = feeSelectorUM.feeNonce is FeeNonce.Nonce,
                ensStatus = getEnsStatus(sendUM),
                fromDerivationIndex = fromDerivationIndex,
                toDerivationIndex = toDerivationIndex,
                feeToken = feeTokenSymbol,
                feeAssetType = feeAssetType,
            ),
        )
        analyticsEventHandler.send(
            Basic.TransactionSent(
                sentFrom = AnalyticsParam.TxSentFrom.Send(
                    blockchain = cryptoCurrency.network.name,
                    token = cryptoCurrency.symbol,
                    feeType = feeType,
                    feeToken = feeTokenSymbol,
                    feeAssetType = feeAssetType,
                ),
                memoType = getSendTransactionMemoType(destinationUM.memoTextField),
            ),
        )
    }

    private fun getSendTransactionMemoType(
        recipientMemo: DestinationTextFieldUM.RecipientMemo?,
    ): Basic.TransactionSent.MemoType {
        val memo = recipientMemo?.value
        return when {
            memo?.isBlank() == true -> Basic.TransactionSent.MemoType.Empty
            memo?.isNotBlank() == true -> Basic.TransactionSent.MemoType.Full
            else -> Basic.TransactionSent.MemoType.Null
        }
    }

    private fun getEnsStatus(sendUM: SendUM): AnalyticsParam.EmptyFull {
        val isBlockchainAddressForEns =
            (sendUM.destinationUM as? DestinationUM.Content)?.addressTextField?.isAddressEns
        return if (isBlockchainAddressForEns == true) {
            AnalyticsParam.EmptyFull.Full
        } else {
            AnalyticsParam.EmptyFull.Empty
        }
    }
}