package com.tangem.features.send.v2.send.confirm

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import arrow.core.Either
import com.tangem.blockchain.common.transaction.TransactionFee
import com.tangem.core.decompose.context.AppComponentContext
import com.tangem.core.decompose.context.child
import com.tangem.core.decompose.model.getOrCreateModel
import com.tangem.core.ui.decompose.ComposableContentComponent
import com.tangem.domain.appcurrency.model.AppCurrency
import com.tangem.domain.models.account.Account
import com.tangem.domain.models.currency.CryptoCurrencyStatus
import com.tangem.domain.models.wallet.UserWallet
import com.tangem.domain.transaction.error.GetFeeError
import com.tangem.features.send.v2.api.FeeSelectorBlockComponent
import com.tangem.features.send.v2.api.SendNotificationsComponent
import com.tangem.features.send.v2.api.SendNotificationsComponent.Params.NotificationData
import com.tangem.features.send.v2.api.analytics.CommonSendAnalyticEvents
import com.tangem.features.send.v2.api.entity.PredefinedValues
import com.tangem.features.send.v2.api.params.FeeSelectorParams
import com.tangem.features.send.v2.api.subcomponents.destination.SendDestinationComponentParams.DestinationBlockParams
import com.tangem.features.send.v2.common.CommonSendRoute
import com.tangem.features.send.v2.common.ui.state.ConfirmUM
import com.tangem.features.send.v2.send.confirm.model.SendConfirmModel
import com.tangem.features.send.v2.send.confirm.ui.SendConfirmContent
import com.tangem.features.send.v2.send.ui.state.SendUM
import com.tangem.features.send.v2.subcomponents.amount.SendAmountBlockComponent
import com.tangem.features.send.v2.subcomponents.amount.SendAmountComponentParams
import com.tangem.features.send.v2.subcomponents.destination.DefaultSendDestinationBlockComponent
import com.tangem.features.send.v2.subcomponents.notifications.DefaultSendNotificationsComponent
import com.tangem.utils.extensions.orZero
import kotlinx.coroutines.flow.*

internal class SendConfirmComponent(
    appComponentContext: AppComponentContext,
    params: Params,
    feeSelectorComponentFactory: FeeSelectorBlockComponent.Factory,
) : ComposableContentComponent, AppComponentContext by appComponentContext {

    private val model: SendConfirmModel = getOrCreateModel(params = params)

    private val blockClickEnableFlow = MutableStateFlow(false)

    private val destinationBlockComponent =
        DefaultSendDestinationBlockComponent(
            appComponentContext = child("sendConfirmDestinationBlock"),
            params = DestinationBlockParams(
                state = model.uiState.value.destinationUM,
                analyticsCategoryName = params.analyticsCategoryName,
                analyticsSendSource = params.analyticsSendSource,
                userWalletId = params.userWallet.walletId,
                cryptoCurrency = params.cryptoCurrencyStatus.currency,
                blockClickEnableFlow = blockClickEnableFlow.asStateFlow(),
                predefinedValues = params.predefinedValues,
            ),
            onResult = model::onDestinationResult,
            onClick = model::showEditDestination,
        )

    private val amountBlockComponent = SendAmountBlockComponent(
        appComponentContext = child("sendConfirmAmountBlock"),
        params = SendAmountComponentParams.AmountBlockParams(
            state = model.uiState.value.amountUM,
            analyticsCategoryName = params.analyticsCategoryName,
            userWallet = params.userWallet,
            appCurrency = params.appCurrency,
            blockClickEnableFlow = blockClickEnableFlow.asStateFlow(),
            predefinedValues = params.predefinedValues,
            userWalletId = params.userWallet.walletId,
            cryptoCurrency = params.cryptoCurrencyStatus.currency,
            cryptoCurrencyStatusFlow = params.cryptoCurrencyStatusFlow,
            isBalanceHidingFlow = params.isBalanceHidingFlow,
            analyticsSendSource = params.analyticsSendSource,
            accountFlow = params.accountFlow,
            isAccountModeFlow = params.isAccountModeFlow,
        ),
        onResult = model::onAmountResult,
        onClick = model::showEditAmount,
    )

    private val feeSelectorBlockComponent = feeSelectorComponentFactory.create(
        context = appComponentContext,
        params = FeeSelectorParams.FeeSelectorBlockParams(
            state = model.uiState.value.feeSelectorUM,
            onLoadFee = params.onLoadFee,
            feeCryptoCurrencyStatus = params.feeCryptoCurrencyStatus,
            cryptoCurrencyStatus = params.cryptoCurrencyStatus,
            feeStateConfiguration = model.feeStateConfiguration,
            feeDisplaySource = FeeSelectorParams.FeeDisplaySource.Screen,
            analyticsCategoryName = params.analyticsCategoryName,
            analyticsSendSource = params.analyticsSendSource,
        ),
        onResult = model::onFeeResult,
    )

    private val notificationsComponent = DefaultSendNotificationsComponent(
        appComponentContext = child("sendConfirmNotifications"),
        params = SendNotificationsComponent.Params(
            analyticsCategoryName = params.analyticsCategoryName,
            userWalletId = params.userWallet.walletId,
            cryptoCurrencyStatus = params.cryptoCurrencyStatus,
            feeCryptoCurrencyStatus = params.feeCryptoCurrencyStatus,
            appCurrency = params.appCurrency,
            callback = model,
            notificationData = NotificationData(
                destinationAddress = model.confirmData.enteredDestination.orEmpty(),
                memo = model.confirmData.enteredMemo,
                amountValue = model.confirmData.enteredAmount.orZero(),
                reduceAmountBy = model.confirmData.reduceAmountBy.orZero(),
                isIgnoreReduce = model.confirmData.isIgnoreReduce,
                fee = model.confirmData.fee,
                feeError = model.confirmData.feeError,
            ),
        ),
    )

    init {
        model.uiState.onEach { state ->
            val confirmUM = state.confirmUM as? ConfirmUM.Content
            blockClickEnableFlow.value = confirmUM?.isSending == false
        }.launchIn(componentScope)
    }

    fun updateState(state: SendUM) {
        destinationBlockComponent.updateState(state.destinationUM)
        amountBlockComponent.updateState(state.amountUM)
        feeSelectorBlockComponent.updateState(state.feeSelectorUM)
        model.updateState(state)
    }

    @Composable
    override fun Content(modifier: Modifier) {
        val state by model.uiState.collectAsStateWithLifecycle()
        val notificationState by notificationsComponent.state.collectAsStateWithLifecycle()

        SendConfirmContent(
            sendUM = state,
            destinationBlockComponent = destinationBlockComponent,
            amountBlockComponent = amountBlockComponent,
            feeSelectorBlockComponent = feeSelectorBlockComponent,
            notificationsComponent = notificationsComponent,
            notificationsUM = notificationState,
        )
    }

    data class Params(
        val state: SendUM,
        val analyticsCategoryName: String,
        val analyticsSendSource: CommonSendAnalyticEvents.CommonSendSource,
        val userWallet: UserWallet,
        val cryptoCurrencyStatus: CryptoCurrencyStatus,
        val feeCryptoCurrencyStatus: CryptoCurrencyStatus,
        val cryptoCurrencyStatusFlow: StateFlow<CryptoCurrencyStatus>,
        val feeCryptoCurrencyStatusFlow: StateFlow<CryptoCurrencyStatus>,
        val accountFlow: StateFlow<Account.CryptoPortfolio?>,
        val isAccountModeFlow: StateFlow<Boolean>,
        val appCurrency: AppCurrency,
        val callback: ModelCallback,
        val currentRoute: Flow<CommonSendRoute>,
        val isBalanceHidingFlow: StateFlow<Boolean>,
        val predefinedValues: PredefinedValues,
        val onLoadFee: suspend () -> Either<GetFeeError, TransactionFee>,
        val onSendTransaction: () -> Unit,
    )

    interface ModelCallback {
        fun onResult(sendUM: SendUM)
    }
}