package com.tangem.features.walletconnect.connections.model.transformers

import com.domain.blockaid.models.dapp.CheckDAppResult
import com.tangem.common.ui.account.PortfolioSelectUM
import com.tangem.domain.models.network.Network
import com.tangem.domain.walletconnect.model.WcSessionProposal
import com.tangem.features.walletconnect.connections.entity.WcAppInfoSecurityNotification
import com.tangem.features.walletconnect.connections.entity.WcAppInfoUM
import com.tangem.features.walletconnect.connections.entity.WcPrimaryButtonConfig
import com.tangem.utils.transformer.Transformer

@Suppress("LongParameterList")
internal class WcAppInfoTransformer(
    private val dAppSession: WcSessionProposal,
    private val dAppVerifiedStateConverter: WcDAppVerifiedStateConverter,
    private val onDismiss: () -> Unit,
    private val onConnect: (securityStatus: CheckDAppResult) -> Unit,
    private val portfolioSelectRow: PortfolioSelectUM,
    private val onNetworksClick: () -> Unit,
    private val proposalNetwork: WcSessionProposal.ProposalNetwork,
    private val additionallyEnabledNetworks: Set<Network>,
) : Transformer<WcAppInfoUM> {

    override fun transform(prevState: WcAppInfoUM): WcAppInfoUM {
        return WcAppInfoUM.Content(
            appName = dAppSession.dAppMetaData.name,
            appIcon = dAppSession.dAppMetaData.icons.firstOrNull().orEmpty(),
            verifiedDAppState = dAppVerifiedStateConverter.convert(
                dAppSession.securityStatus to dAppSession.dAppMetaData.name,
            ),
            appSubtitle = WcAppSubtitleConverter.convert(dAppSession.dAppMetaData),
            notification = createNotification(dAppSession.securityStatus),
            portfolioSelectRow = portfolioSelectRow,
            networksInfo = WcNetworksInfoConverter.convert(
                value = WcNetworksInfoConverter.Input(
                    missingNetworks = proposalNetwork.missingRequired,
                    requiredNetworks = proposalNetwork.required,
                    availableNetworks = proposalNetwork.available,
                    notAddedNetworks = proposalNetwork.notAdded,
                    additionallyEnabledNetworks = additionallyEnabledNetworks,
                ),
            ),
            onNetworksClick = onNetworksClick,
            connectButtonConfig = WcPrimaryButtonConfig(
                showProgress = false,
                enabled = WcConnectButtonAvailabilityConverter.convert(
                    WcConnectButtonAvailabilityConverter.Input(
                        missingNetworks = proposalNetwork.missingRequired,
                        requiredNetworks = proposalNetwork.required,
                        availableNetworks = proposalNetwork.available,
                        selectedNetworks = additionallyEnabledNetworks,
                    ),
                ),
                onClick = { onConnect(dAppSession.securityStatus) },
            ),
            onDismiss = onDismiss,
        )
    }

    private fun createNotification(securityStatus: CheckDAppResult): WcAppInfoSecurityNotification? {
        return when (securityStatus) {
            CheckDAppResult.SAFE -> null
            CheckDAppResult.UNSAFE -> WcAppInfoSecurityNotification.SecurityRisk
            CheckDAppResult.FAILED_TO_VERIFY -> WcAppInfoSecurityNotification.UnknownDomain
        }
    }
}