package com.tangem.feature.wallet.presentation.wallet.state.transformers

import com.tangem.blockchain.common.Blockchain
import com.tangem.core.ui.components.transactions.state.TxHistoryState
import com.tangem.domain.card.common.util.cardTypesResolver
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.models.network.TxInfo
import com.tangem.domain.models.wallet.UserWallet
import com.tangem.domain.txhistory.models.TxHistoryStateError
import com.tangem.feature.wallet.child.wallet.model.intents.WalletClickIntents
import com.tangem.feature.wallet.presentation.wallet.state.model.WalletState
import com.tangem.feature.wallet.presentation.wallet.state.model.WalletUM
import com.tangem.feature.wallet.presentation.wallet.state.transformers.converter.TxHistoryItemStateConverter
import kotlinx.collections.immutable.toImmutableList
import com.tangem.utils.logging.TangemLogger

internal class SetTxHistoryCountErrorTransformer(
    private val userWallet: UserWallet,
    private val error: TxHistoryStateError,
    private val pendingTransactions: Set<TxInfo>,
    private val clickIntents: WalletClickIntents,
    private val currency: CryptoCurrency,
) : WalletStateTransformer(userWallet.walletId) {

    private val txHistoryItemConverter by lazy {
        val blockchain = when (userWallet) {
            is UserWallet.Cold -> userWallet.scanResponse.cardTypesResolver.getBlockchain()
            is UserWallet.Hot -> Blockchain.Unknown
        }

        TxHistoryItemStateConverter(
            currency = currency,
            symbol = blockchain.currency,
            decimals = blockchain.decimals(),
            clickIntents = clickIntents,
        )
    }

    override fun transform(walletUM: WalletUM): WalletUM {
        return walletUM // todo redesign main
    }

    override fun transform(prevState: WalletState): WalletState {
        return when (prevState) {
            is WalletState.SingleCurrency.Content -> prevState.copy(txHistoryState = createErrorState())
            is WalletState.SingleCurrency.Locked,
            is WalletState.MultiCurrency,
            -> {
                TangemLogger.w("Impossible to load transactions history for multi-currency wallet")
                prevState
            }
        }
    }

    private fun createErrorState(): TxHistoryState = when (error) {
        is TxHistoryStateError.EmptyTxHistories -> {
            TxHistoryState.Empty(onExploreClick = clickIntents::onExploreClick)
        }
        is TxHistoryStateError.DataError -> {
            TxHistoryState.Error(
                onReloadClick = clickIntents::onReloadClick,
                onExploreClick = clickIntents::onExploreClick,
            )
        }
        is TxHistoryStateError.TxHistoryNotImplemented -> {
            TxHistoryState.NotSupported(
                pendingTransactions = txHistoryItemConverter.convertList(pendingTransactions)
                    .toImmutableList(),
                onExploreClick = clickIntents::onExploreClick,
            )
        }
    }
}