package com.tangem.features.send.send.success.ui

import androidx.compose.animation.*
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import com.tangem.common.ui.amountScreen.ui.AmountBlock
import com.tangem.core.ui.components.Fade
import com.tangem.core.ui.components.SpacerH
import com.tangem.core.ui.components.transactions.TransactionDoneTitle
import com.tangem.core.ui.extensions.resourceReference
import com.tangem.core.ui.extensions.wrappedList
import com.tangem.core.ui.res.TangemTheme
import com.tangem.core.ui.test.TransactionSuccessScreenTestTags
import com.tangem.core.ui.utils.DateTimeFormatters
import com.tangem.core.ui.utils.toPx
import com.tangem.core.ui.utils.toTimeFormat
import com.tangem.features.send.api.subcomponents.destination.SendDestinationBlockComponent
import com.tangem.features.send.common.ui.FeeBlockSuccess
import com.tangem.features.send.common.ui.state.ConfirmUM
import com.tangem.features.send.impl.R
import com.tangem.features.send.send.ui.state.SendUM

@Composable
internal fun SendConfirmSuccessContent(sendUM: SendUM, destinationBlockComponent: SendDestinationBlockComponent) {
    var isVisible by remember { mutableStateOf(false) }

    LaunchedEffect(Unit) {
        isVisible = true
    }

    val height = ANIMATION_OFFSET.toPx().toInt()

    AnimatedVisibility(
        visible = isVisible,
        enter = slideInVertically(
            initialOffsetY = { height },
        ).plus(fadeIn()),
        exit = slideOutVertically().plus(fadeOut()),
        label = "Animate success content",
    ) {
        Column {
            Box(
                modifier = Modifier
                    .weight(1f)
                    .background(TangemTheme.colors.background.tertiary)
                    .testTag(TransactionSuccessScreenTestTags.CONTAINER),
            ) {
                SuccessContent(
                    sendUM = sendUM,
                    destinationBlockComponent = destinationBlockComponent,
                    modifier = Modifier.fillMaxHeight(),
                )
                Fade(
                    modifier = Modifier.align(Alignment.BottomCenter),
                    backgroundColor = TangemTheme.colors.background.tertiary,
                )
            }
        }
    }
}

@Composable
private fun SuccessContent(
    sendUM: SendUM,
    destinationBlockComponent: SendDestinationBlockComponent,
    modifier: Modifier = Modifier,
) {
    Column(
        modifier = modifier
            .padding(horizontal = 16.dp)
            .verticalScroll(rememberScrollState()),
        verticalArrangement = Arrangement.spacedBy(12.dp),
    ) {
        if (sendUM.confirmUM is ConfirmUM.Success) {
            TransactionDoneTitle(
                title = resourceReference(R.string.sent_transaction_sent_title),
                subtitle = resourceReference(
                    R.string.send_date_format,
                    wrappedList(
                        sendUM.confirmUM.transactionDate.toTimeFormat(DateTimeFormatters.dateFormatter),
                        sendUM.confirmUM.transactionDate.toTimeFormat(),
                    ),
                ),
                modifier = Modifier.padding(vertical = 12.dp),
            )
        }
        AmountBlock(
            amountState = sendUM.amountUM,
            isClickDisabled = true,
            isEditingDisabled = true,
            onClick = {},
        )
        destinationBlockComponent.Content(modifier = Modifier)
        FeeBlockSuccess(feeSelectorUM = sendUM.feeSelectorUM)
        SpacerH(16.dp)
    }
}

private val ANIMATION_OFFSET = (-40).dp