package com.tangem.features.addressbook.addaddress.ui

import android.content.res.Configuration
import androidx.compose.animation.*
import androidx.compose.animation.core.snap
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.tangem.core.ui.R
import com.tangem.core.ui.components.SpacerH
import com.tangem.core.ui.ds.button.TangemButtonType
import com.tangem.core.ui.ds.button.TangemButtonUM
import com.tangem.core.ui.ds2.button.TangemButton
import com.tangem.core.ui.ds2.topnavigation.TangemTopNavigation
import com.tangem.core.ui.extensions.TextReference
import com.tangem.core.ui.extensions.resourceReference
import com.tangem.core.ui.extensions.stringResourceSafe
import com.tangem.core.ui.res.TangemTheme
import com.tangem.core.ui.res.TangemThemePreviewRedesign
import com.tangem.features.addressbook.addaddress.ui.state.AddAddressUM
import com.tangem.features.addressbook.addaddress.ui.state.AddressFieldUM

@Composable
internal fun AddAddressContent(state: AddAddressUM, modifier: Modifier = Modifier) {
    Column(
        modifier = modifier
            .fillMaxSize()
            .background(color = TangemTheme.colors3.bg.primary)
            .navigationBarsPadding(),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        TangemTopNavigation(
            title = state.title,
            contentAlign = TangemTopNavigation.ContentAlign.Center,
            blurBackground = false,
            onBack = state.onBackClick,
        )
        BoxWithConstraints(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f),
        ) {
            val minContentHeight = maxHeight
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .verticalScroll(rememberScrollState()),
            ) {
                Column(
                    modifier = Modifier
                        .heightIn(min = minContentHeight)
                        .imePadding(),
                ) {
                    RecipientRow(
                        modifier = Modifier.padding(horizontal = 16.dp),
                        addressField = state.addressField,
                        onValueChange = state.onAddressChange,
                        onAddressClear = state.onAddressClear,
                        onQrClick = state.onQrClick,
                        onPasteClick = state.onPasteClick,
                    )
                    MemoSection(memoField = state.memoField)
                    NetworkSelector(
                        chosenNetworkStateUM = state.chosenNetworkStateUM,
                        onNetworkClick = state.onNetworkClick,
                    )
                    AddButton(state.buttonUM)
                }
            }
        }
    }
}

@Composable
private fun MemoSection(memoField: AddAddressUM.MemoFieldUM) {
    AnimatedVisibility(
        visible = memoField.isVisible,
        enter = fadeIn() + expandVertically(),
        exit = fadeOut() + shrinkVertically(),
    ) {
        Column(modifier = Modifier.fillMaxWidth()) {
            MemoRow(
                modifier = Modifier
                    .padding(horizontal = 16.dp)
                    .padding(top = 12.dp),
                memoField = memoField,
            )
            SpacerH(10.dp)
            Text(
                modifier = Modifier.padding(horizontal = 32.dp),
                text = stringResourceSafe(R.string.send_recipient_memo_footer_v2),
                style = TangemTheme.typography3.caption.medium,
                color = TangemTheme.colors3.text.secondary,
            )
            Text(
                modifier = Modifier.padding(horizontal = 32.dp),
                text = stringResourceSafe(R.string.send_recipient_memo_footer_v2_highlighted),
                style = TangemTheme.typography3.caption.medium,
                color = TangemTheme.colors3.text.primary,
            )
        }
    }
}

@Composable
private fun NetworkSelector(chosenNetworkStateUM: AddAddressUM.ChosenNetworkStateUM, onNetworkClick: () -> Unit) {
    AnimatedContent(
        targetState = chosenNetworkStateUM,
        transitionSpec = {
            ContentTransform(
                targetContentEnter = fadeIn(),
                initialContentExit = fadeOut(),
                sizeTransform = SizeTransform(clip = false) { _, _ -> snap() },
            )
        },
        contentKey = { it::class },
        modifier = Modifier.animateContentSize(),
        label = "network_selector",
    ) { networkState ->
        when (networkState) {
            AddAddressUM.ChosenNetworkStateUM.Hidden -> Box(modifier = Modifier.fillMaxWidth())
            AddAddressUM.ChosenNetworkStateUM.Loading,
            AddAddressUM.ChosenNetworkStateUM.SelectNetwork,
            is AddAddressUM.ChosenNetworkStateUM.Result,
            -> NetworkBlock(
                modifier = Modifier
                    .padding(horizontal = 16.dp)
                    .padding(top = 16.dp)
                    .clip(RoundedCornerShape(16.dp))
                    .fillMaxWidth()
                    .background(color = TangemTheme.colors3.bg.secondary),
                chosenNetworkStateUM = networkState,
                onNetworkSelectClick = onNetworkClick,
            )
        }
    }
}

@Composable
private fun ColumnScope.AddButton(buttonUM: TangemButtonUM) {
    Spacer(modifier = Modifier.weight(1f))
    TangemButton(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 12.dp),
        onClick = buttonUM.onClick,
        isEnabled = buttonUM.isEnabled,
        isLoading = buttonUM.isLoading,
        size = TangemButton.Size.X12,
        text = buttonUM.text,
    )
}

@Preview(showBackground = true, widthDp = 360)
@Preview(showBackground = true, widthDp = 360, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
private fun Preview_AddAddressContent() {
    TangemThemePreviewRedesign {
        AddAddressContent(
            state = AddAddressUM(
                addressField = AddressFieldUM(
                    value = "",
                    placeholder = resourceReference(R.string.address_book_enter_address),
                    label = resourceReference(R.string.common_address),
                ),
                memoField = AddAddressUM.MemoFieldUM(
                    isVisible = false,
                    value = "",
                    label = resourceReference(R.string.send_extras_hint_memo),
                    isError = false,
                    onValueChange = {},
                    onPasteClick = {},
                ),
                buttonUM = TangemButtonUM(
                    text = TextReference.Res(R.string.address_book_add_address),
                    type = TangemButtonType.Primary,
                    isEnabled = false,
                    onClick = { },
                ),
                chosenNetworkStateUM = AddAddressUM.ChosenNetworkStateUM.Hidden,
                onAddressChange = {},
                onAddressClear = {},
                onPasteClick = {},
                onQrClick = {},
                onBackClick = {},
                onNetworkClick = {},
                title = resourceReference(R.string.address_book_add_address),
            ),
        )
    }
}